我整天都在努力理解错误。我是一个学习者,第一次尝试自定义适配器和自定义对象类。请帮助我理解我的错误。我尝试了很多次冲浪,直到最终在这里输入我的怀疑。任何帮助将不胜感激。从网站上学习后,我已经尝试过了。我遵循相同的步骤,但无法解决此问题。日志猫说
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.miwok, PID: 7814
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miwok/com.example.miwok.NumbersActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2487)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.miwok.NumbersActivity.onCreate(NumbersActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6355)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) package com.example.miwok;
这是NumbersActivity
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intial);
ArrayList<Word>words1=new ArrayList<Word>();
words1.add(new Word("One","lutti"));
words1.add(new Word("Two","ottiko"));
words1.add(new Word("Three","tolookosu"));
words1.add(new Word("Four","oyyisa"));
words1.add(new Word("Five","massokka"));
words1.add(new Word("Six","temmokka"));
words1.add(new Word("seven","kenukaku"));
words1.add(new Word("Eight","kawinta"));
words1.add(new Word("Nine","wo'e"));
words1.add(new Word("Ten","na'aacha"));
WordAdapter itemsAdapter=new WordAdapter(this, words1);
ListView listview=(ListView) findViewById(R.id.word_list);
listview.setAdapter(itemsAdapter);
}
}
WordAdapter在这里
package com.example.miwok;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<Word> {
private static final String LOG_TAG = WordAdapter.class.getSimpleName();
public WordAdapter(Activity context,ArrayList<Word> words){
super(context,0,words);
}
@Override
public View getView(int position, @NonNull View convertView, @NonNull ViewGroup parent) {
View listItemview=convertView;
if(listItemview==null)
{
listItemview=LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
Word current=getItem(position);
TextView txt=(TextView)listItemview.findViewById(R.id.text_view);
txt.setText(current.getDefaultTranslation());
TextView txt1=(TextView)listItemview.findViewById(R.id.miwok_translation);
txt1.setText(current.getMiwokTranslation());
return listItemview;
}
}
这是Java文件一词
package com.example.miwok;
public class Word {
private String mMiwokTranslation;
private String mDefaultTranslation;
public Word(String vDefaultTranslation, String vMiwokTranslation) {
mMiwokTranslation = vMiwokTranslation;
mDefaultTranslation = vDefaultTranslation;
}
public String getDefaultTranslation() {
return mDefaultTranslation;
}
public String getMiwokTranslation() {
return mMiwokTranslation;
}
}
请帮帮我。
答案 0 :(得分:0)
NullPointerException
表示您正在尝试访问不存在的内容。
由于:java.lang.NullPointerException:尝试调用虚拟 方法“无效” android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用 在com.example.miwok.NumbersActivity.onCreate(NumbersActivity.java:30)
您尚未检索到ListView。您确定使用的是正确的xml文件吗?是正确的ID吗?