AsyncTask onPostExecute错误的新意图

时间:2017-03-03 04:28:55

标签: java android android-intent android-asynctask

我想在AsyncTask onPostExecute中调用意图但我收到错误,

这是我的代码,

protected void onPostExecute(Void aVoid) {
   if(name.equals("success")){
      // tv.setText("berhasil");
      // Toast.makeText(mContext, name, Toast.LENGTH_LONG).show();
       Intent intent = new Intent(MainActivity.this, SecondActivity.class);
       getApplicationContext().startActivity(intent);
   } else {
       pDialog.setMessage(name);
       pDialog.show();
   }
   pDialog.dismiss();
   super.onPostExecute(aVoid);
}

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

任何人都可以帮我解决这个问题吗?

之前谢谢

此错误日志

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: aws.sample.com.sampleawsconnection, PID: 9959
              java.lang.NoSuchMethodError: No static method setOnApplyWindowInsetsListener(Landroid/view/View;Landroid/support/v4/view/ViewCompatLollipop$OnApplyWindowInsetsListenerBridge;)V in class Landroid/support/v4/view/ViewCompatLollipop; or its super classes (declaration of 'android.support.v4.view.ViewCompatLollipop' appears in /data/app/aws.sample.com.sampleawsconnection-2/base.apk)
                  at android.support.v4.view.ViewCompat$LollipopViewCompatImpl.setOnApplyWindowInsetsListener(ViewCompat.java:1646)
                  at android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(ViewCompat.java:2996)
                  at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:425)
                  at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                  at aws.sample.com.sampleawsconnection.SecondActivity.onCreate(SecondActivity.java:15)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

3 个答案:

答案 0 :(得分:0)

如果没有FLAG_ACTIVITY_NEW_TASK

,您无法从应用程序上下文启动活动

两个选项(1)将您的活动上下文传递给任务(错误,导致泄漏)或(2)添加标志:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

编辑:我刚刚发现您可以访问MainActivity.this这意味着您已成为内部课程,因此您可以这样做:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);

请确保您的gradle文件是最新的(您可能需要同步最新的构建工具):

compileSdkVersion 25
targetSdkVersion to 25

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'

答案 1 :(得分:0)

将旧库更新为较新的库。 像

compile 'com.android.support:design:23.1.1'

compile 'com.android.support:design:24.2.0'

并将compileSdkVersion更新为24,将buildToolsVersion更新为24.0.1,将targetSdkVersion更新为24。

希望它有所帮助。

答案 2 :(得分:-1)

在Asynctask的构造函数中传递上下文对象,并将该上下文用于postexecute中的startActivity