Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的自定义阵列列表每次启动时都会崩溃我不确定为什么会一直崩溃_Java_Android - Fatal编程技术网

Java 我的自定义阵列列表每次启动时都会崩溃我不确定为什么会一直崩溃

Java 我的自定义阵列列表每次启动时都会崩溃我不确定为什么会一直崩溃,java,android,Java,Android,我的代码不断崩溃我不确定到底出了什么问题我只知道arraylist的调用者没有接收到它我感觉好像适配器出了问题。android studio也不例外,因为它让应用程序运行,应用程序打开,然后崩溃。调试器是这么说的。应用程序运行到arraylist中的返回行,然后应用程序崩溃 //主要活动// package com.example.reportcardapp; import androidx.appcompat.app.AppCompatActivity; import android.os

我的代码不断崩溃我不确定到底出了什么问题我只知道arraylist的调用者没有接收到它我感觉好像适配器出了问题。android studio也不例外,因为它让应用程序运行,应用程序打开,然后崩溃。调试器是这么说的。应用程序运行到arraylist中的返回行,然后应用程序崩溃

//主要活动//

package com.example.reportcardapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grade_list);


    ArrayList<Grade> grades = new ArrayList<Grade>();


    grades.add(new Grade("Name: Jake;", "Math;", 'A'));
    grades.add(new Grade("Name: Jake;", "Science", 'C'));
    grades.add(new Grade("Name: Jake;", "Gym", 'B'));
    grades.add(new Grade("Name: Jake;", "Web Design", 'c'));
    grades.add(new Grade("Name: Jake;", "Music", 'F'));

    ReportCardAdapter aadapter = new ReportCardAdapter(this, grades);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(aadapter);

}

}
//成绩单添加器//

package com.example.reportcardapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import java.util.ArrayList;


 public class ReportCardAdapter extends ArrayAdapter<Grade> {


public ReportCardAdapter(Context context, ArrayList<Grade> grades) {
    super(context, 0, grades);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)        {
    // Check if an existing view is being reused, otherwise inflate   the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    // Get the {@link Word} object located at this position in the   list
    Grade currentGrade = getItem(position);


    TextView nameTextView = listItemView.findViewById(R.id.name);
    nameTextView.setText(currentGrade.getName());


    TextView classNameTextView = listItemView.findViewById(R.id.className);
    classNameTextView.setText(currentGrade.getClassName());


    TextView gradeLetterTextView = listItemView.findViewById(R.id.letterGrade);
    gradeLetterTextView.setText(currentGrade.getGradeLetter());
    return listItemView;
}
}

看起来R.id.name不存在。检查strings.xml文件

请发布错误消息/异常堆栈跟踪!未注明日期的问题会弹出一个带有更多上下文的错误,您不能使用任意整数值调用setText。getGradeLetter方法返回一个字符,该字符会自动转换为int,以适合最接近的方法签名。首先需要将其转换为字符串;e、 例如,gradeLetterTextView.setTextString.valueOfcurrentGrade.getGradeLetter;。谢谢你,迈克,这对我很有帮助。理解了为什么它也会崩溃
public class Grade {
private String mname;
private String mclassName;
private char mgradeLetter;


public Grade(String name, String className, char gradeLetter) {
    mname = name;
    mclassName = className;
    mgradeLetter = gradeLetter;
}

public String getName() {
    return mname;
}

public String getClassName() {
    return mclassName;
}

public char getGradeLetter() {
    return mgradeLetter;
}
}
package com.example.reportcardapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import java.util.ArrayList;


 public class ReportCardAdapter extends ArrayAdapter<Grade> {


public ReportCardAdapter(Context context, ArrayList<Grade> grades) {
    super(context, 0, grades);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)        {
    // Check if an existing view is being reused, otherwise inflate   the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }

    // Get the {@link Word} object located at this position in the   list
    Grade currentGrade = getItem(position);


    TextView nameTextView = listItemView.findViewById(R.id.name);
    nameTextView.setText(currentGrade.getName());


    TextView classNameTextView = listItemView.findViewById(R.id.className);
    classNameTextView.setText(currentGrade.getClassName());


    TextView gradeLetterTextView = listItemView.findViewById(R.id.letterGrade);
    gradeLetterTextView.setText(currentGrade.getGradeLetter());
    return listItemView;
}
}