2017-05-22 88 views
-3

我在我的java代码中收到错误Nullpointerexception。这里是我的堆栈跟踪:在Java中的settext的NullPointerException异常android

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


    } 
} 

这里是我的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> 

而且我的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> 

显然,错误来自我j的最后一行ava代码“tvSelection.setText(text);”。请帮助我对我的代码有什么问题感到困惑。

回答

2

在XML代码

android:text="@+id/superHerosTV" 

应该

android:id="@+id/superHerosTV" 

记住findViewById(...)将返回空值,如果有给定id没有意见。这是因为你的代码不起作用。

+0

非常感谢你! – Mona

1

问题是这样的线:

android:text="@+id/superHerosTV" 

它应该是:

android:id="@+id/superHerosTV" 

tvSelection =(TextView的)findViewById(R.id.superHerosTV)将始终为空,因为该视图可以” t被发现

+0

非常感谢你! – Mona

相关问题