java android中settext的NullPointerException
我在java代码中得到了错误Nullpointerexception。这是我的stacktrace:java android中settext的NullPointerException,java,android,Java,Android,我在java代码中得到了错误Nullpointerexception。这是我的stacktrace: --------- beginning of crash 05-22 19:59:56.285 3050-3050/com.example.it3783.listview E/AndroidRuntime: FATAL EXCEPTION: main
--------- beginning of crash
05-22 19:59:56.285 3050-3050/com.example.it3783.listview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.listview, PID: 3050
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example..listview.MainActivity$1.onItemClick(MainActivity.java:50)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以下是我的java代码:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<CharSequence>adapter; //define adapter
TextView tvSelection; //define the superhero display text
ListView lvLocation; //define the location in listview
String []strArray; // string array for the 4 locations
String []superHeros = {"1. Batman\n 2. SpiderMan", "1. Peter Pan\n2. Superman",
"1. IronMan\n2. Peter Pan", "1. SnowWhite \n2. Gingerbreadman"};
//string for the superhero names
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.locationArea, android.R.layout.simple_list_item_1);
Resources myRes = this.getResources();
strArray = myRes.getStringArray(R.array.locationArea);
//create an adapter linked to the objects of the place
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, strArray);
//superheroes to find the superhero linked to location
tvSelection = (TextView) findViewById(R.id.superHerosTV);
lvLocation = (ListView) findViewById(R.id.spLocations);
//locations to click on location
//Link the listview to the objects of the location
lvLocation.setAdapter(adapter);
lvLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = superHeros[position];
tvSelection.setText(text);
}
});
}
}
public类MainActivity扩展了AppCompatActivity{
ArrayAdapteradapter;//定义适配器
TextView tvSelection;//定义超级英雄显示文本
ListView lvLocation;//在ListView中定义位置
String[]strArray;//4个位置的字符串数组
String[]超级英雄={“1.蝙蝠侠\n 2.蜘蛛侠”,“1.彼得·潘\n 2.超人”,
“1.铁人\n2.彼得·潘”,“1.白雪公主\n2.姜饼人”};
//超级英雄名字的字符串
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter=ArrayAdapter.createFromResource(这个,R.array.locationArea,android.R.layout.simple\u list\u item\u 1);
Resources myRes=this.getResources();
strArray=myRes.getStringArray(R.array.locationArea);
//创建链接到场所对象的适配器
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,strArray);
//超级英雄找到超级英雄链接到的位置
tvSelection=(文本视图)findViewById(R.id.superHerosTV);
lvLocation=(ListView)findViewById(R.id.spLocations);
//要单击位置的位置
//将listview链接到位置的对象
lvLocation.setAdapter(适配器);
lvLocation.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串文本=超级英雄[位置];
tvSelection.setText(文本);
}
});
}
}
以下是我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.listview.MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose a location..."/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:id="@+id/spLocations"></ListView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/superHerosTV"
/>
</LinearLayout>
和my strings.xml:
<resources>
<string name="app_name">ListView</string>
<string-array name="locationArea">
<item>North</item>
<item>South</item>
<item>East</item>
<item>West</item>
</string-array>
</resources>
列表视图
北
南方
东
西
显然,错误来自java代码的最后一行“tvSelection.setText(text);”。请帮助我,我不知道我的代码出了什么问题。在您的XML代码中
android:text="@+id/superHerosTV"
应该是
android:id="@+id/superHerosTV"
请记住,如果没有具有给定id的视图,
findViewById(…)
将返回null。这是因为您的代码不工作。问题是这一行:
android:text="@+id/superHerosTV"
应该是:
android:id="@+id/superHerosTV"
tvSelection=(TextView)findViewById(R.id.superHerosTV)将始终为空,因为找不到该视图