App Force在启动ListView类

时间:2017-10-05 15:38:43

标签: android listview android-intent

在我的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 ActivityRecord{28c80a2 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_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.test.mvsaditya.testapp.activityAtwo">

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

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

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

Android资源和骆驼案似乎相处得不好。尝试将ActivityATwo.XML重命名为activity_atwo.xml,就像在Activity类中命名它一样。

答案 1 :(得分:0)

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

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_width="match_parent"
                      android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/subjectList"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </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_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</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();
    }
}

请尝试上面的代码,它将完美地工作。