2017-05-22 3 views
-2

アンドロイドエミュレータを使用してコードを実行していたときに、「残念ながらリストビューが機能しなくなりました」という問題が発生しました。 私のコードを見てくれてありがとう! MainActivity.javaについては残念ながら、リストビューが動作しなくなりました(アンドロイドエラー)

<?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.it3783.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については

public class MainActivity extends AppCompatActivity { 

    ArrayAdapter<CharSequence>adapter; 
    TextView tvSelection; 
    ListView lvLocation; 

    String []strArray; 
    String []superHeros = {"1. Batman\n 2. SpiderMan", "1. Peter Pan\n2. Superman", 
      "1. IronMan\n2. Peter Pan", "1. SnowWhite \n2. Gingerbreadman"}; 

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

     adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, strArray); 

     tvSelection = (TextView) findViewById(R.id.superHerosTV); 
     lvLocation = (ListView) findViewById(R.id.spLocations); 

     Resources myRes = this.getResources(); 
     strArray = myRes.getStringArray(R.array.locationArea); 

     lvLocation.setAdapter(adapter); 

     lvLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String text = strArray[position]; 
       tvSelection.setText(text); 
      } 
     }); 

     adapter = ArrayAdapter.createFromResource(this, R.array.locationArea, android.R.layout.simple_list_item_1); 
    } 
} 

<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> 
+0

ここにスタックトレースを入れてください –

+0

[残念ながらMyAppは停止しました。どうすればこの問題を解決できますか?](http://stackoverflow.com/questions/23353173) –

答えて

0
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1, strArray); 

があなたのonCreateメソッドでこの行が取るactivity_main.xmlについては

strArray(初期化されていないので、値i sヌル。 nullデータのアダプタをlistviewに設定すると、クラッシュします。

関連する問題