我想从post执行启动另一个Activity并将一些参数传递给它。但它强制关闭应用程序。这是我的onPostExecute()
:
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
imageView.setImageBitmap(result);
imageView.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
Toast.makeText(context, ""+captionStr, Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, Article.class);
Bundle b = new Bundle();
b.putString("url", "Hello"); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
// context.finish();*/
}
});
} else {
imageView.setImageResource(R.drawable.ic_launcher);
}
}
我的宣言声明
<activity android:name=".com.tdodemo.tdo.Article" />
LogCat
09-04 10:53:30.759: E/AndroidRuntime(15546): FATAL EXCEPTION: main
09-04 10:53:30.759: E/AndroidRuntime(15546): Process: com.tdodemo.tdo, PID: 15546
09-04 10:53:30.759: E/AndroidRuntime(15546): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.app.ContextImpl.startActivity(ContextImpl.java:1146)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.app.ContextImpl.startActivity(ContextImpl.java:1133)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.content.ContextWrapper.startActivity(ContextWrapper.java:328)
09-04 10:53:30.759: E/AndroidRuntime(15546): at com.tdodemo.tdo.ImageLoadTask$1.onClick(ImageLoadTask.java:82)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.view.View.performClick(View.java:4562)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.view.View$PerformClick.run(View.java:18918)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.os.Handler.handleCallback(Handler.java:808)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.os.Handler.dispatchMessage(Handler.java:103)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.os.Looper.loop(Looper.java:193)
09-04 10:53:30.759: E/AndroidRuntime(15546): at android.app.ActivityThread.main(ActivityThread.java:5388)
09-04 10:53:30.759: E/AndroidRuntime(15546): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 10:53:30.759: E/AndroidRuntime(15546): at java.lang.reflect.Method.invoke(Method.java:515)
09-04 10:53:30.759: E/AndroidRuntime(15546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-04 10:53:30.759: E/AndroidRuntime(15546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
09-04 10:53:30.759: E/AndroidRuntime(15546): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试这样的事情:
Intent intent_name = new Intent();
intent_name.setClass(getApplicationContext(),Article.class);
Bundle b = new Bundle();
b.putString("url", "Hello"); //Your id
intent_name.putExtras(b);
startActivity(intent_name);
OR
由于你的AsyncTask的一个参数是你可以直接使用的活动
Intent intent_name = new Intent(activity, Article.class);
Bundle b = new Bundle();
b.putString("url", "Hello"); //Your id
intent_name.putExtras(b);
activity.startActivity();
希望这可以帮到你!
答案 1 :(得分:0)
尝试使用您的活动名称。
Intent intent = new Intent(YOUR ACTIIVITY NAME.this , Article.class);
startActivity(intent);
答案 2 :(得分:0)
您需要添加标志 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intent = new Intent(context, Article.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle b = new Bundle();
b.putString("url", "Hello"); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
答案 3 :(得分:0)
通过查看你的logcat来说明所需的标志。通过在你的意图中添加那个标志来尝试它
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
答案 4 :(得分:0)
尝试使用tis代码,tis正在为我工作
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
Intent intent = new Intent(MyAsyncTaskActivity.this, NextActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context().startActivity(intent);
}
答案 5 :(得分:-1)
试试这个,移动这个
imageView.setImageBitmap(result);
imageView.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
Toast.makeText(context, ""+captionStr, Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, Article.class);
Bundle b = new Bundle();
b.putString("url", "Hello"); //Your id
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
// context.finish();*/
}
});`
在onPostExecute之外,在调用asyncTask类之后调用主活动。 onPostExecute将图像保存到drawable目录,将imageView更改为
imageView.setImageBitmap(res.drawable.Image);