Android中的getActivity,getContext,getView未定义

时间:2016-12-21 16:16:10

标签: android greendao

我在项目中使用了MVP模式,定义了一个在View界面中显示进度对话框的方法,并在演示者中使用它。在编译时输出以下错误消息:

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;
    }
}

活动代码:

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'

我尝试在片段中调用getActivity()方法和getContext()方法,并且Google MVP示例代码中发布的当前代码提供了使用片段getView()方法的方法,仍然编译错误。我在项目中使用GreenDAO,看到Gradle信息的汇编,在发生错误时执行greendao任务,但我不知道如何解决这个问题,希望你能帮助我,谢谢

2 个答案:

答案 0 :(得分:0)

我有类似的问题。在我的情况下,问题是我导入的“import org.greenrobot.greendao.annotation.ToMany”,尽管它从未在代码中使用过。根据我的想法,在你的情况下,问题是由于“import org.greenrobot.greendao.annotation.NotNull”。

希望这有帮助。

答案 1 :(得分:0)

我已经解决了这个问题,我将GreenDao数据实体放在一个单独的模块中,然后就可以编译了。