gpt4 book ai didi

java - AsyncTask onPostExecute 错误中的新 Intent

转载 作者:行者123 更新时间:2023-11-29 09:27:58 26 4
gpt4 key购买 nike

我想在 AsyncTask onPostExecute 中调用 intent,但出现错误,

这是我的代码,

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

第二个 Activity .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)

最佳答案

如果没有 FLAG_ACTIVITY_NEW_TASK

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

两个选项 (1) 将您的 Activity 上下文传递给任务(不好,导致泄漏)或 (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'

关于java - AsyncTask onPostExecute 错误中的新 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42570673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com