Android 片段中的 getActivity、getContext、getView 未定义
Android getActivity, getContext, getView in fragment is undefined
我在项目中使用了MVP模式,定义了一个在View界面显示进度对话框的方法,并在presenter中使用。编译时输出如下错误信息:
Executing tasks: [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug]
Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.
:app:clean
:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareComAndroidSupportAnimatedVectorDrawable2501Library
:app:prepareComAndroidSupportAppcompatV72501Library
:app:prepareComAndroidSupportDesign2501Library
:app:prepareComAndroidSupportRecyclerviewV72501Library
:app:prepareComAndroidSupportSupportCompat2501Library
:app:prepareComAndroidSupportSupportCoreUi2501Library
:app:prepareComAndroidSupportSupportCoreUtils2501Library
:app:prepareComAndroidSupportSupportFragment2501Library
:app:prepareComAndroidSupportSupportMediaCompat2501Library
:app:prepareComAndroidSupportSupportV42501Library
:app:prepareComAndroidSupportSupportVectorDrawable2501Library
:app:prepareComAndroidSupportTransition2501Library
:app:prepareComJiongbullJlog105Library
:app:prepareDeHdodenhofCircleimageview210Library
:app:prepareIoReactivexRxandroid121Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:mockableAndroidJar UP-TO-DATE
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoCore222Library
:app:prepareComAndroidSupportTestEspressoEspressoIdlingResource222Library
:app:prepareComAndroidSupportTestExposedInstrumentationApiPublish05Library
:app:prepareComAndroidSupportTestRules05Library
:app:prepareComAndroidSupportTestRunner05Library
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:generateDebugAndroidTestResValues
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:greendaoPrepare
:app:greendao
Found 8 problem(s) parsing "/Users/xujianzhe/workspace/oa/app/src/main/java/oa/function/welcome_page/WelcomeFragment.java":
#0 @52: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#1 @53: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#2 @60: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#3 @61: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#4 @68: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#5 @69: The method getContext() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#6 @70: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#7 @71: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:greendao'.
Found 8 problem(s) parsing "/Users/xujianzhe/workspace/oa/app/src/main/java/oa/function/welcome_page/WelcomeFragment.java". First problem:
Pb(100) The method getView() is undefined for the type WelcomeFragment (67108964 at line 52
Run gradle with --info for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.973 secs
片段代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.greenrobot.greendao.annotation.NotNull;
/**
* A simple {@link Fragment} subclass.
*/
public class WelcomeFragment extends Fragment implements WelcomeContract.View{
private WelcomeContract.Presenter mPresenter;
public WelcomeFragment() {
// Required empty public constructor
}
public static WelcomeFragment getInstance() {
return new WelcomeFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_welcome, container, false);
}
@Override
public void onResume() {
super.onResume();
this.mPresenter.prepareData();
}
@Override
public void setPresenter(@NotNull WelcomeContract.Presenter presenter) {
this.mPresenter = presenter;
}
@Override
public void showProgressBar() {
if (getView() != null) {
((WelcomeActivity)getView().getContext()).showProgressDialog();
}
}
@Override
public void hideProgressBar() {
if (getView() != null) {
((WelcomeActivity)getView().getContext()).hideProgressDialog();
}
}
@Override
public void startLoginPage() {
if (getView() != null) {
Intent intent = new Intent(getContext(), LoginActivity.class);
getView().getContext().startActivity(intent);
((WelcomeActivity)getView().getContext()).finish();
}
}
}
合约代码:
public interface WelcomeContract {
interface View extends BaseView<Presenter> {
void showProgressBar();
void hideProgressBar();
void startLoginPage();
}
interface Presenter extends BasePresenter {
void prepareData();
}
}
主持人代码:
import android.support.annotation.NonNull;
import com.jiongbull.jlog.JLog;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
/**
* Created by xujianzhe on 2016/12/21.
*/
public class WelcomePresenter implements WelcomeContract.Presenter {
private WelcomeContract.View mView;
private CompositeSubscription mCompositeSubscription;
private boolean isPreparing;
public WelcomePresenter(@NonNull WelcomeContract.View view) {
this.mView = view;
this.mView.setPresenter(this);
mCompositeSubscription = new CompositeSubscription();
}
@Override
public void prepareData() {
if (isPreparing) {
return;
}
J
isPreparing = true;
mView.showProgressBar();
//create temp account
AccountInfo accountInfo = new AccountInfo();
accountInfo.setAccountName("100001");
accountInfo.setAccountPassword("1");
Subscription subscription = Observable.just(accountInfo)
.subscribeOn(Schedulers.newThread())
.map(new Func1<AccountInfo, Void>() {
@Override
public Void call(AccountInfo accountInfo) {
AccountInfoDao accountInfoDao = BaseApp.instance.getDaoSession().getAccountInfoDao();
if (accountInfoDao.loadAll().size() <= 0 ) {
accountInfoDao.insert(accountInfo);
}
return null;
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mView.hideProgressBar();
isPreparing = false;
}
});
mCompositeSubscription.add(subscription);
}
@Override
public void subscribe() {
}
@Override
public void unSubscribe() {
mCompositeSubscription.unsubscribe();
}
public boolean isPreparing() {
return isPreparing;
}
}
Activity代码:
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public class WelcomeActivity extends BaseActivity {
private ProgressDialog progressDialog;
private WelcomePresenter mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentView);
WelcomeFragment mWelcomeFragment;
if (fragment == null) {
mWelcomeFragment = WelcomeFragment.getInstance();
getSupportFragmentManager().beginTransaction().add(R.id.contentView, mWelcomeFragment).commitAllowingStateLoss();
} else {
mWelcomeFragment = (WelcomeFragment) fragment;
}
mPresenter = new WelcomePresenter(mWelcomeFragment);
}
void showProgressDialog() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
if (!progressDialog.isShowing()) {
progressDialog.show();
}
}
void hideProgressDialog() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.hide();
}
}
@Override
protected void onResume() {
super.onResume();
mPresenter.subscribe();
if (mPresenter.isPreparing()) {
showProgressDialog();
}
}
@Override
protected void onPause() {
super.onPause();
mPresenter.unSubscribe();
}
@Override
protected void onStop() {
super.onStop();
hideProgressDialog();
}
}
app/build.gradle
apply plugin: 'org.greenrobot.greendao'
// orm
compile "org.greenrobot:greendao:3.2.0"
build.gradle
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
我尝试在Fragment中调用getActivity()
方法和getContext()
方法,GoogleMVP示例代码中贴出的当前代码提供了使用Fragment的方法getView()
方法,还是编译错误。我在项目中使用了GreenDAO
,看到了编译Gradle的信息,在执行greendao task的时候出现了这个错误,但是不知道如何解决这个问题,希望大家帮忙我,谢谢你
我遇到了类似的问题。在我的例子中,问题是我在导入时遇到的 "import org.greenrobot.greendao.annotation.ToMany",尽管它从未在代码中使用过。基于 tha,我猜你的问题是由于 "import org.greenrobot.greendao.annotation.NotNull"。
希望对您有所帮助。
我已经解决了这个问题,我把GreenDao数据实体放在一个单独的模块中,然后你就可以编译了。
我在项目中使用了MVP模式,定义了一个在View界面显示进度对话框的方法,并在presenter中使用。编译时输出如下错误信息:
Executing tasks: [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug]
Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.
:app:clean
:app:buildInfoDebugLoader
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:prepareComAndroidSupportAnimatedVectorDrawable2501Library
:app:prepareComAndroidSupportAppcompatV72501Library
:app:prepareComAndroidSupportDesign2501Library
:app:prepareComAndroidSupportRecyclerviewV72501Library
:app:prepareComAndroidSupportSupportCompat2501Library
:app:prepareComAndroidSupportSupportCoreUi2501Library
:app:prepareComAndroidSupportSupportCoreUtils2501Library
:app:prepareComAndroidSupportSupportFragment2501Library
:app:prepareComAndroidSupportSupportMediaCompat2501Library
:app:prepareComAndroidSupportSupportV42501Library
:app:prepareComAndroidSupportSupportVectorDrawable2501Library
:app:prepareComAndroidSupportTransition2501Library
:app:prepareComJiongbullJlog105Library
:app:prepareDeHdodenhofCircleimageview210Library
:app:prepareIoReactivexRxandroid121Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:mockableAndroidJar UP-TO-DATE
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoCore222Library
:app:prepareComAndroidSupportTestEspressoEspressoIdlingResource222Library
:app:prepareComAndroidSupportTestExposedInstrumentationApiPublish05Library
:app:prepareComAndroidSupportTestRules05Library
:app:prepareComAndroidSupportTestRunner05Library
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:generateDebugAndroidTestResValues
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:greendaoPrepare
:app:greendao
Found 8 problem(s) parsing "/Users/xujianzhe/workspace/oa/app/src/main/java/oa/function/welcome_page/WelcomeFragment.java":
#0 @52: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#1 @53: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#2 @60: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#3 @61: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#4 @68: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#5 @69: The method getContext() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#6 @70: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
#7 @71: The method getView() is undefined for the type WelcomeFragment (ID: 67108964; error: true)
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:greendao'.
Found 8 problem(s) parsing "/Users/xujianzhe/workspace/oa/app/src/main/java/oa/function/welcome_page/WelcomeFragment.java". First problem:
Pb(100) The method getView() is undefined for the type WelcomeFragment (67108964 at line 52
Run gradle with --info for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.973 secs
片段代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.greenrobot.greendao.annotation.NotNull;
/**
* A simple {@link Fragment} subclass.
*/
public class WelcomeFragment extends Fragment implements WelcomeContract.View{
private WelcomeContract.Presenter mPresenter;
public WelcomeFragment() {
// Required empty public constructor
}
public static WelcomeFragment getInstance() {
return new WelcomeFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_welcome, container, false);
}
@Override
public void onResume() {
super.onResume();
this.mPresenter.prepareData();
}
@Override
public void setPresenter(@NotNull WelcomeContract.Presenter presenter) {
this.mPresenter = presenter;
}
@Override
public void showProgressBar() {
if (getView() != null) {
((WelcomeActivity)getView().getContext()).showProgressDialog();
}
}
@Override
public void hideProgressBar() {
if (getView() != null) {
((WelcomeActivity)getView().getContext()).hideProgressDialog();
}
}
@Override
public void startLoginPage() {
if (getView() != null) {
Intent intent = new Intent(getContext(), LoginActivity.class);
getView().getContext().startActivity(intent);
((WelcomeActivity)getView().getContext()).finish();
}
}
}
合约代码:
public interface WelcomeContract {
interface View extends BaseView<Presenter> {
void showProgressBar();
void hideProgressBar();
void startLoginPage();
}
interface Presenter extends BasePresenter {
void prepareData();
}
}
主持人代码:
import android.support.annotation.NonNull;
import com.jiongbull.jlog.JLog;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
/**
* Created by xujianzhe on 2016/12/21.
*/
public class WelcomePresenter implements WelcomeContract.Presenter {
private WelcomeContract.View mView;
private CompositeSubscription mCompositeSubscription;
private boolean isPreparing;
public WelcomePresenter(@NonNull WelcomeContract.View view) {
this.mView = view;
this.mView.setPresenter(this);
mCompositeSubscription = new CompositeSubscription();
}
@Override
public void prepareData() {
if (isPreparing) {
return;
}
J
isPreparing = true;
mView.showProgressBar();
//create temp account
AccountInfo accountInfo = new AccountInfo();
accountInfo.setAccountName("100001");
accountInfo.setAccountPassword("1");
Subscription subscription = Observable.just(accountInfo)
.subscribeOn(Schedulers.newThread())
.map(new Func1<AccountInfo, Void>() {
@Override
public Void call(AccountInfo accountInfo) {
AccountInfoDao accountInfoDao = BaseApp.instance.getDaoSession().getAccountInfoDao();
if (accountInfoDao.loadAll().size() <= 0 ) {
accountInfoDao.insert(accountInfo);
}
return null;
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
mView.hideProgressBar();
isPreparing = false;
}
});
mCompositeSubscription.add(subscription);
}
@Override
public void subscribe() {
}
@Override
public void unSubscribe() {
mCompositeSubscription.unsubscribe();
}
public boolean isPreparing() {
return isPreparing;
}
}
Activity代码:
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
public class WelcomeActivity extends BaseActivity {
private ProgressDialog progressDialog;
private WelcomePresenter mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentView);
WelcomeFragment mWelcomeFragment;
if (fragment == null) {
mWelcomeFragment = WelcomeFragment.getInstance();
getSupportFragmentManager().beginTransaction().add(R.id.contentView, mWelcomeFragment).commitAllowingStateLoss();
} else {
mWelcomeFragment = (WelcomeFragment) fragment;
}
mPresenter = new WelcomePresenter(mWelcomeFragment);
}
void showProgressDialog() {
if (progressDialog == null) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(true);
}
if (!progressDialog.isShowing()) {
progressDialog.show();
}
}
void hideProgressDialog() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.hide();
}
}
@Override
protected void onResume() {
super.onResume();
mPresenter.subscribe();
if (mPresenter.isPreparing()) {
showProgressDialog();
}
}
@Override
protected void onPause() {
super.onPause();
mPresenter.unSubscribe();
}
@Override
protected void onStop() {
super.onStop();
hideProgressDialog();
}
}
app/build.gradle
apply plugin: 'org.greenrobot.greendao'
// orm
compile "org.greenrobot:greendao:3.2.0"
build.gradle
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
我尝试在Fragment中调用getActivity()
方法和getContext()
方法,GoogleMVP示例代码中贴出的当前代码提供了使用Fragment的方法getView()
方法,还是编译错误。我在项目中使用了GreenDAO
,看到了编译Gradle的信息,在执行greendao task的时候出现了这个错误,但是不知道如何解决这个问题,希望大家帮忙我,谢谢你
我遇到了类似的问题。在我的例子中,问题是我在导入时遇到的 "import org.greenrobot.greendao.annotation.ToMany",尽管它从未在代码中使用过。基于 tha,我猜你的问题是由于 "import org.greenrobot.greendao.annotation.NotNull"。
希望对您有所帮助。
我已经解决了这个问题,我把GreenDao数据实体放在一个单独的模块中,然后你就可以编译了。