即使在使用异步任务后,当我尝试传递第二个意图时,我的应用程序也会退出并显示错误消息

Posted

技术标签:

【中文标题】即使在使用异步任务后,当我尝试传递第二个意图时,我的应用程序也会退出并显示错误消息【英文标题】:My app is exiting with an error message when I'm trying to pass the 2nd intent even after using Async task 【发布时间】:2017-11-14 19:31:34 【问题描述】:

Logcat 从主屏幕传递第二个intent 时,它正在显示

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d09b20)
06-14 11:16:24.513 4507-4507/com.infinity.wall_to_paper E/androidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.infinity.wall_to_paper, PID: 4507
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfocom.infinity.wall_to_paper/com.infinity.wall_to_paper.Settings: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:136)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:515)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                              at dalvik.system.NativeStart.main(Native Method)
                                                                           Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                              at com.infinity.wall_to_paper.Settings.onCreate(Settings.java:22)
                                                                              at android.app.Activity.performCreate(Activity.java:5231)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:136) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                                           Caused by: java.lang.reflect.InvocationTargetException
                                                                              at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                              at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:594)
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                              at com.infinity.wall_to_paper.Settings.onCreate(Settings.java:22) 
                                                                              at android.app.Activity.performCreate(Activity.java:5231) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:136) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 
                                                                           Caused by: java.lang.OutOfMemoryError
                                                                              at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                              at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
                                                                              at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
                                                                              at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                                                                              at android.content.res.Resources.loadDrawable(Resources.java:2110)
                                                                              at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                                                                              at android.view.View.<init>(View.java:3554)
                                                                              at android.view.ViewGroup.<init>(ViewGroup.java:470)
                                                                              at android.widget.LinearLayout.<init>(LinearLayout.java:176)
                                                                              at android.widget.LinearLayout.<init>(LinearLayout.java:172)
                                                                              at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                              at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
                                                                              at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                              at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                              at com.infinity.wall_to_paper.Settings.onCreate(Settings.java:22) 
                                                                              at android.app.Activity.performCreate(Activity.java:5231) 
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                                              at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:136) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 

我的计划是:

public class HomeScreen extends AppCompatActivity

ImageView show_cat,settings;
     @Override

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //MainScreen
    show_cat=(ImageView)findViewById(R.id.show_cat);
    settings=(ImageView)findViewById(R.id.setting_show_fragment);
   

    public void showCat(View view)

     //on click method
     new MyCustomAsyncTask(this).execute();


public void showSettings(View view)
    //on click method
    Intent i= new Intent(this, Settings.class);
    startActivity(i);
    

我正在使用Async task

公共类 MyCustomAsyncTask 扩展 AsyncTask 私有上下文上下文;

public MyCustomAsyncTask(Context context)
    this.context=context;


@Override
protected void onPreExecute() 
    // write show progress Dialog code here
    super.onPreExecute();


@Override
protected Void doInBackground(Void... params) 
    // write service code here
    return null;


@Override
protected void onPostExecute(Void aVoid) 
    super.onPostExecute(aVoid);
    Intent intent = new Intent(context, Categories.class);
    context.startActivity(intent);


    

类别的布局文件是:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_ android:layout_
   android:background="@drawable/wallpaper_category">
    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/sports"
        android:src="@drawable/sports"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/sports"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/nature"
        android:layout_alignStart="@+id/nature"
        android:onClick="openSports" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/love"
        android:src="@drawable/love"
        android:layout_above="@+id/movies"
        android:layout_toLeftOf="@+id/sports"
        android:layout_toStartOf="@+id/sports"
        android:contentDescription="@string/love"
        android:onClick="openLove" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/movies"
        android:src="@drawable/movies"
        android:layout_alignBottom="@+id/nature"
        android:layout_toLeftOf="@+id/nature"
        android:layout_toStartOf="@+id/nature"
        android:contentDescription="@string/movies"
        android:onClick="openMovies" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/superhero"
        android:src="@drawable/superhero"
        android:layout_below="@+id/movies"
        android:layout_alignRight="@+id/movies"
        android:layout_alignEnd="@+id/movies"
        android:contentDescription="@string/superhero"
        android:onClick="openSuperhero" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/cartoons"
        android:src="@drawable/cartoons"
        android:focusable="false"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/cartoons"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:focusableInTouchMode="false"
        android:onClick="openCartoons" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/artistic"
        android:src="@drawable/artistic"
        android:layout_alignTop="@+id/nature"
        android:layout_alignLeft="@+id/animals"
        android:layout_alignStart="@+id/animals"
        android:contentDescription="@string/artistic"
        android:onClick="openArtistic" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/animals"
        android:layout_gravity="center_vertical|right"
        android:src="@drawable/animals"
        android:layout_above="@+id/artistic"
        android:layout_toRightOf="@+id/sports"
        android:layout_toEndOf="@+id/sports"
        android:contentDescription="@string/animals"
        android:onClick="openAnimals" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/vehicles"
        android:src="@drawable/vehicles"
        android:layout_below="@+id/artistic"
        android:layout_alignLeft="@+id/artistic"
        android:layout_alignStart="@+id/artistic"
        android:contentDescription="@string/vehicles"
        android:onClick="openVehicles" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/nature"
        android:src="@drawable/nature"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/cartoons"
        android:layout_alignStart="@+id/cartoons"
        android:contentDescription="@string/nature"
        android:onClick="openNature" />

    <ImageView
        android:layout_
        android:layout_
        android:id="@+id/back_to_main"
        android:contentDescription="@string/back_going"
        android:background="@drawable/back_going"
        android:onClick="onClickBack"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/sports"
        android:layout_alignEnd="@+id/sports"
        android:layout_marginRight="220dp" />

【问题讨论】:

您可以发布您的 Categories.class 的布局文件吗? 请edit问题包括其余的膨胀异常 你真的认为将 Activity 上下文传递给 AsyncTask 是个好主意吗,这是要求内存泄漏? @DeepakSingh 先生,我已经添加了布局文件 @cricket_007 先生,我已经添加了完整的错误消息 【参考方案1】:

您设置为背景的图像 drawable/wallpaper_category 导致 Out of Memory 异常。

删除布局文件的第 2 行,它将运行而不会崩溃。

您需要使用 Glide 或 Picasso 来有效地加载图像。

【讨论】:

以上是关于即使在使用异步任务后,当我尝试传递第二个意图时,我的应用程序也会退出并显示错误消息的主要内容,如果未能解决你的问题,请参考以下文章

如何取消 useEffect 清理函数中的所有订阅和异步任务?

尝试保存第二个任务时服务的“并发”错误

onClick 中的第二个意图在第一个之前执行

在不同的活动中将数据从 ListView 传递到 TextView,Android

在python中将方法从辅助GUI传递到主GUI

当我尝试使用图像按钮进入第二个活动时应用程序崩溃