启动 ListView 类时应用程序强制关闭

Posted

技术标签:

【中文标题】启动 ListView 类时应用程序强制关闭【英文标题】:App force closes upon launching ListView Class 【发布时间】:2018-03-17 07:27:44 【问题描述】:

在我的 android 项目中,我有一个 ListView 适配器类。当我在单击按钮/项目时尝试启动此活动时,应用程序由于某种原因而崩溃。

我正在尝试使用 Intent 从 MainActivity.java 启动 ActivityATwo.java,点击后必须执行 Intent。

我在 MainActivity.java 的 XML 文件中添加了 onClick 属性

MainActivity.java

import com.test.mvsaditya.testapp.*;
import com.test.mvsaditya.testapp.activityAtwo;
.
.
.
   public void onSubjects ( MenuItem item)
    
        Intent toSubjects = new Intent ( MainActivity.this, activityAtwo.class);
        startActivity(toSubjects);

    

ActivityATwo.java :(这是项目中的一个模块,gradle构建成功)

package com.test.mvsaditya.testapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.LinearLayoutManager;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import java.util.*;

public class activityAtwo extends AppCompatActivity 

    private final String sub_names[] = 
            "Math",
            "Chem",
            "Physics",
            "Bio",
            "DBMS"
    ;

    private final String sub_descs[] = 
            "Numbers and shiz",
            "Gravity and charges and shiz",
            "Elements and shiz",
            "Shiz",
            "shizzz"
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atwo);
       RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.subjectList);
        mRecyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);

        ArrayList<Subjects> subjectList = prepareData();

        MyAdapter adapter= new MyAdapter(getApplicationContext(),subjectList);
        mRecyclerView.setAdapter(adapter);


    

    private ArrayList<Subjects> prepareData()

        ArrayList<Subjects> sub = new ArrayList<>();
        for(int i=0;i<sub_names.length;i++)
            Subjects subObj = new Subjects(sub_names[i],sub_descs[i]);
            sub.add(subObj);
        
        return sub;
    

这是我点击按钮后得到的:

10-05 20:57:45.550 4750-4750/com.grs.raja.collegeapp D/AndroidRuntime: Shutting down VM
10-05 20:57:45.550 4750-4750/com.grs.raja.collegeapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.grs.raja.collegeapp, PID: 4750
                                                                       java.lang.NoSuchFieldError: No field subjectList of type I in class Lcom/test/mvsaditya/testapp/R$id; or its superclasses (declaration of 'com.test.mvsaditya.testapp.R$id' appears in /data/app/com.grs.raja.collegeapp-1/split_lib_slice_0_apk.apk)
                                                                           at com.test.mvsaditya.testapp.activityAtwo.onCreate(activityAtwo.java:48)
                                                                           at android.app.Activity.performCreate(Activity.java:6662)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
10-05 20:57:45.551 1647-3063/system_process W/ActivityManager:   Force finishing activity com.grs.raja.collegeapp/com.test.mvsaditya.testapp.activityAtwo
10-05 20:57:45.553 1647-3063/system_process W/ActivityManager:   Force finishing activity com.grs.raja.collegeapp/com.example.praharsha.myapplication.MainActivity
10-05 20:57:45.593 1647-4415/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
10-05 20:57:45.593 1647-4415/system_process D/OpenGLRenderer: Swap behavior 1
10-05 20:57:45.594 1647-4415/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
10-05 20:57:45.594 1647-4415/system_process D/OpenGLRenderer: Swap behavior 0
10-05 20:57:45.652 1647-4415/system_process D/EGL_emulation: eglCreateContext: 0xa3b426c0: maj 3 min 1 rcv 4
10-05 20:57:45.777 1647-4415/system_process D/EGL_emulation: eglMakeCurrent: 0xa3b426c0: ver 3 1 (tinfo 0x97fb51e0)
10-05 20:57:45.778 1647-4415/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
10-05 20:57:45.778 1647-4415/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
10-05 20:57:45.778 1647-4415/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
10-05 20:57:45.925 1647-4415/system_process D/EGL_emulation: eglMakeCurrent: 0xa3b426c0: ver 3 1 (tinfo 0x97fb51e0)
10-05 20:57:46.053 1647-1663/system_process W/ActivityManager: Activity pause timeout for ActivityRecord28c80a2 u0 com.grs.raja.collegeapp/com.test.mvsaditya.testapp.activityAtwo t10 f
10-05 20:57:48.710 1391-1415/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 3370790 , only wrote 3062160
10-05 20:57:55.514 1647-1663/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
10-05 20:58:00.006 1746-2038/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa8205240: ver 3 1 (tinfo 0xa82032d0)

这是 activity_atwo.XML 布局:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        android:fitsSystemWindows="true"
        tools:context="com.test.mvsaditya.testapp.activityAtwo">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/subjectList"
            android:scrollbars="vertical"
            android:layout_
            android:layout_/>

    </android.support.design.widget.CoordinatorLayout>

ListView 适配器:

package com.test.mvsaditya.testapp;

import android.content.Context;
import android.media.Image;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by mvsaditya on 20-09-2017.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.SubHolder>

    public ArrayList<Subjects> mDataSet;    private Context context;


    public static class SubHolder extends RecyclerView.ViewHolder
    
       //private ImageView mItemImage;
        private TextView mItemName;
         ImageButton fileicon;
        //private TextView mItemDescription;


        //private static final String PHOTO_KEY = "PHOTO";

        public SubHolder(final View v)
            super(v);

           //mItemDescription = (TextView)v.findViewById(R.id.item_description);
            mItemName = (TextView)v.findViewById(R.id.item_name);



    

    public void onClick(View v)
    

    


    

    public MyAdapter(Context context, ArrayList<Subjects> myDataSet)
       this.context = context;
        mDataSet=myDataSet;
    


    public MyAdapter.SubHolder onCreateViewHolder(ViewGroup parent, int viewType) 

       View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_row, parent, false);
        return new SubHolder(inflatedView);
    

    @Override
    public void onBindViewHolder(SubHolder holder, int position) 
        holder.mItemName.setText(mDataSet.get(position).Subname);
        //holder.mItemDescription.setText(mDataSet.get(position).Subdescription);

    

    @Override
    public int getItemCount()
        return mDataSet.size();
    

如何解决这个问题?

【问题讨论】:

你能告诉我们activityAtwo.java的第48行吗 @ErnestoUlloa RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.subjectList); 看来通过阅读这部分错误:java.lang.NoSuchFieldError: No field subjectList of type I in class Lcom/test/mvsaditya/testapp/R$id; at com.test.mvsaditya.testapp.activityAtwo.onCreate(activityAtwo.java:48) that is not finding a view with that id on the layout activity_atwo.xml 检查或发布您的布局。 @ErnestoUlloa 我在底部添加了布局代码。请检查 我可以看看你的适配器吗?查看 onClickEvent 分配 【参考方案1】:

请尝试以下代码在我的应用中正常工作。

1. demo.xml
<?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:orientation="vertical"
                      android:layout_
                      android:layout_>
            <android.support.v7.widget.RecyclerView
                android:id="@+id/subjectList"
                android:scrollbars="vertical"
                android:layout_
                android:layout_/>
        </LinearLayout>

2. recycler_view_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_
              android:layout_>

    <TextView
        android:id="@+id/item_name"
        android:layout_
        android:layout_/>
</LinearLayout>

3. Subjects.java
class Subjects 
    String ss;
    String sv;

    public Subjects(String aSsa, String aSva) 
        ss = aSsa;
        sv = aSva;
    

    public String getSv() 
        return sv;
    

    public void setSv(String aSva) 
        sv = aSva;
    

    public String getSs() 
        return ss;
    

    public void setSs(String aSsa) 
        ss = aSsa;
    


4. ActivityAtwo.java
public class ActivityAtwo extends AppCompatActivity 

    private final String sub_names[] = 
            "Math",
            "Chem",
            "Physics",
            "Bio",
            "DBMS"
    ;

    private final String sub_descs[] = 
            "Numbers and shiz",
            "Gravity and charges and shiz",
            "Elements and shiz",
            "Shiz",
            "shizzz"
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);
       RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.subjectList);
        mRecyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);

        ArrayList<Subjects> subjectList = prepareData();

        MyAdapter adapter= new MyAdapter(getApplicationContext(),subjectList);
        mRecyclerView.setAdapter(adapter);


    

    private ArrayList<Subjects> prepareData()

        ArrayList<Subjects> sub = new ArrayList<>();
        for(int i=0;i<sub_names.length;i++)
            Subjects subObj = new Subjects(sub_names[i],sub_descs[i]);
            sub.add(subObj);
        
        return sub;
    


5. MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.SubHolder> 
    public ArrayList<Subjects> mDataSet;
    private Context context;


    public static class SubHolder extends RecyclerView.ViewHolder 
        //private ImageView mItemImage;
        private TextView mItemName;
        ImageButton fileicon;
        //private TextView mItemDescription;


        //private static final String PHOTO_KEY = "PHOTO";

        public SubHolder(final View v) 
            super(v);

            //mItemDescription = (TextView)v.findViewById(R.id.item_description);
            mItemName = (TextView) v.findViewById(R.id.item_name);


        

        public void onClick(View v) 

        


    

    public MyAdapter(Context context, ArrayList<Subjects> myDataSet) 
        this.context = context;
        mDataSet = myDataSet;
    


    public MyAdapter.SubHolder onCreateViewHolder(ViewGroup parent, int viewType) 

        View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_row, parent, false);
        return new SubHolder(inflatedView);
    

    @Override
    public void onBindViewHolder(SubHolder holder, int position) 
        holder.mItemName.setText(mDataSet.get(position).getSs());
        //holder.mItemDescription.setText(mDataSet.get(position).Subdescription);

    

    @Override
    public int getItemCount() 
        return mDataSet.size();
    

请尝试上面的代码,它会完美运行。

【讨论】:

当我尝试使用 Intent 从另一个活动启动 ActivityATwo.java 时,应用程序被强制关闭。你能帮我解决这个问题吗?【参考方案2】:

Android 资源和骆驼案似乎相处得并不融洽。尝试将 ActivityATwo.XML 重命名为 activity_atwo.xml,就像您在 Activity 类中命名的那样。

【讨论】:

以上是关于启动 ListView 类时应用程序强制关闭的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在“强制关闭”后自动重启应用程序?

启动屏幕后应用程序强制关闭

应用程序强制在启动时关闭

应用程序在启动 facebook 活动时强制关闭

添加启动画面后Android应用程序强制关闭

使用异步任务将数据显示到列表视图时强制关闭应用程序