Android GetActivity,GetContext,fragment中的GetView是不确定的



我使用了项目中的MVP模式,定义了一种在视图接口中显示进度对话框的方法,并将其在主持人中使用。以下错误消息在编译时间输出:

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任务,但我不知道如何解决此问题,希望您能帮助我,谢谢您

我也有类似的问题。就我而言,问题是我正在进口的"导入org.greenrobot.greendao.annotation.tomany",尽管它从未在代码中使用过。我想在您的情况下,问题是由于"导入org.greenrobot.greendao.annotation.notnull"。

希望这会有所帮助。

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

相关内容

  • 没有找到相关文章

最新更新