NullPointerException错误尝试在空对象引用上调用虚拟方法错误

时间:2018-11-20 01:30:29

标签: android nullpointerexception logcat

我正在编写语言学习程序。我正在做的是对每组单词进行测试。 没有编译错误;但是,根据我的logcat,适配器上存在nullpointerexception。我不确定为什么会这样。

我的适配器

if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(this.layout, parent, false);
        holder.answer = (EditText)convertView.findViewById(R.id.answer);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }

    holder.ref = position;

    final EditText answer = (EditText)convertView.findViewById(R.id.answer);
    final Test listViewItem = TestList.get(position);

    holder.answer.setText(listViewItem.getAns());

    if(listItem == null)
        listItem = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);

    Test currenttest = TestList.get(position);

    TextView Qunum = (TextView) listItem.findViewById(R.id.Qnum);
    Qunum.setText(currenttest.getQnum());

    TextView Kore = (TextView) listItem.findViewById(R.id.Korean1);
    Kore.setText(currenttest.getK());

    TextView Prono = (TextView) listItem.findViewById(R.id.Pronounciation);
    Prono.setText(currenttest.getPron());

    return listItem;

}

需要意图的活动

getScore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(int i=0;i<20;i++){
                View view=listview.getChildAt(i);
                EditText editText=view.findViewById(R.id.answer);
                String string = editText.getText().toString();
                inputans.add(string);
            }
            count = 0;
            for(int x = 0; x < 20; x++){
                if(answers.get(x) == inputans.get(x)){
                    count++;
                }
            }
            Intent intent = new Intent(numbertest.this, dicselect.class);
            intent.putExtra("score", count);
            if(count<10){
                intent.putExtra("message", "Work Harder !!");
                startActivityForResult(intent, 1);
            }
            else if(count<=15){
                intent.putExtra("message", "Great !!");
                startActivityForResult(intent, 1);
            }
            else{
                intent.putExtra("message", "Well Done !!");
                startActivityForResult(intent, 1);
            }
            startActivity(intent);
        }
    });
我的自定义列表视图的项目的

xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="390dp"
    android:layout_height="50dp"
    android:orientation = "horizontal">

    <TextView
        android:id="@+id/Qnum"
        android:text=""
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:textSize="20dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop = "4dp"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/Korean1"
        android:text=""
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:textSize = "20dp"/>

    <TextView
        android:text=""
        android:id="@+id/Pronounciation"
        android:layout_width="180dp"
        android:layout_height="40dp"
        android:layout_gravity = "center"
        android:layout_marginLeft = "4dp"
        android:textSize = "20dp"/>
</LinearLayout>

<LinearLayout
    android:layout_width="390dp"
    android:layout_height="50dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_marginLeft="2dp"
        android:text = "Answer: "
        android:layout_gravity="center"
        android:textSize = "25dp"
        android:textColor = "#000000"
        />

    <EditText
        android:id="@+id/answer"
        android:layout_width="270dp"
        android:layout_height="40dp"
        android:textSize="20dp"
        android:layout_marginLeft="5dp"
        android:layout_gravity="center"
        />

</LinearLayout>

LogCat java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.TextView.setText(java.lang.CharSequence)”         在com.example.rkdtm.koreanlearner.TestAdapter.getView(TestAdapter.java:73)         在android.widget.AbsListView.obtainView(AbsListView.java:2366)         在android.widget.ListView.makeAndAddView(ListView.java:2052)

1 个答案:

答案 0 :(得分:0)

我不确定this.layout是什么,但无论是什么,它都应该是您的商品布局XML资源:

convertView = LayoutInflater.from(parent.getCo).inflate(R.layout.row, parent, false);

您不需要给listItem充气,因为我认为它与convertView相同:

TextView Qunum = (TextView) convertView.findViewById(R.id.Qnum);
Qunum.setText(currenttest.getQnum());

TextView Kore = (TextView) convertView.findViewById(R.id.Korean1);
Kore.setText(currenttest.getK());

TextView Prono = (TextView) convertView.findViewById(R.id.Pronounciation);
Prono.setText(currenttest.getPron());

以防万一,请确保在TextView上设置文本时不要使用Integer