2016-12-21 8 views
0

私はプロジェクトでMVPパターンを使用し、Viewインターフェイスで進行ダイアログを表示する方法を定義し、プレゼンターで使用しました。次のエラーメッセージがコンパイル時に出力される。Android getActivity、getContext、フラグメント内のgetViewは定義されていません

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' 

IフラグメントでgetActivity()方法及びgetContext()メソッドを呼び出すことを試み、そして現在のコードは、に転記Google MVPサンプルコードは、フラグメントgetView()メソッドを使用する方法を提供しますが、エラーはまだコンパイルされます。私はプロジェクトでGreenDAOを使用して、エラーが発生したときにgreendaoタスクの実装では、Gradle情報のコンパイルを参照してくださいが、私はこの問題を解決する方法を知らない、あなたが私を助けることを願って、ありがとう

答えて

0

Iこの問題を解決してきた、私は別のモジュールでGreenDaoデータ実体を入れ、その後、あなたがコンパイルすることができます。

0

私も同様の問題がありました。私のケースでは、問題は "インポートorg.greenrobot.greendao.annotation.ToMany"でしたが、コードでは使用されていませんでしたが、インポートしていました。私はあなたのケースでは、問題は "org.greenrobot.greendao.annotation.NotNullをインポートする"ためです。

これが役に立ちます。

+0

ありがとうございましたが、私はこの問題を解決してきた、私は別のモジュールでGreenDaoデータ実体を置きます、そしてコンパイルすることができます。 – XvJianzhe

関連する問題