2016-10-19 14 views
0

私はリストビューのカスタムレイアウトと、res/stringsから引き出された文字列配列を使用しようとしています。最初に私は使用しようとしましたAndroidスタジオ:ArrayAdapterがリストビューで発生する

adapter = ArrayAdapter.createFromResource(this,R.array.android_versions,R.layout.list_view_custom) 

しかし、エミュレータがシャットダウンするテキストビューのリソースIDを指定する必要があるというエラーメッセージが表示されました。だから私は文字列の配列を取得するために、4行目を使用して、以下のコードで見られるnewArrayAdapterを使ってみました。カスタムレイアウトを使用する前にこれがすべて機能していたので、この問題は4行目、9行目、10行目のどこかにあると思います。

public class ListViewActivity extends AppCompatActivity { 
    ListView listview; 
    ArrayAdapter<String> adapter; 
    List<String> android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions)); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_view); 
     listview = (ListView)findViewById(R.id.listview); 
     adapter = new ArrayAdapter<>(this,R.layout.list_view_custom,R.id.listview_custom,android_versions); 
     listview.setAdapter(adapter); 
     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " is selected",Toast.LENGTH_LONG).show(); 
      } 
     }); 

    } 

} 

ここでは、activity_list_viewで取得されたListViewActivity、content_list_view。のレイアウトを示します。ここで

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.andrewtakao.testapp.ListViewActivity" 
    tools:showIn="@layout/activity_list_view"> 
    <ListView 
     android:id="@+id/listview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    </ListView> 

</RelativeLayout> 

私はListViewコントロールここ

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/listview_custom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/colorPrimary" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="@string/apple" 
     android:gravity="center_horizontal"/> 

</LinearLayout> 

そして最後に実装しようとしていレイアウトが私の最新のエラーメッセージが表示されている。

-18 18:51:17.682 12567- 12567/com.example.andrewtakao.testapp I/Choreographer:112フレームをスキップしました!アプリケーションがメインスレッドで多くの作業を行っている可能性があります。 10-18 18:55:11.808 12567-12567/com.example.andrewtakao.testapp D/AndroidRuntime:シャットダウン VMダウン10-18 18:55:11.809 12567-12567/com.example.andrewtakao.testapp E/AndroidRuntime:致命的例外:メイン プロセス:com.example.andrewtakao.testapp、PID:12567 java.lang.RuntimeException:アクティビティをインスタンス化できません ComponentInfo {com.example.andrewtakao.testapp/com.example.andrewtakao.testapp。 ListViewActivity}: java.lang.NullPointerExceptionが仮想メソッドを呼び出す試み 'android.content.res.Resourcesのandroid.content.Context.getResources()' ヌルオブジェクト参照 にandroid.appで android.appで.ActivityThread.performLaunchActivity android.app.ActivityThread.-wrap11で(ActivityThread.java:2327) でandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) (ActivityThread.java) 。 ActivityThread $ H.handleMessage(ActivityThread.java:1344) とandroid.os.Handler.dispatchMessage(Handler.java:102) (android.os.Looper.loop(Looper.java:148) android.app)にあります。 ActivityThread.main(ActivityThread.java:5417) (java.lang.reflect.Method.invoke)(ネイティブメソッド) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:java.lang.NullPointerException : でandroid.content.ContextWrapper.getResources(ContextWrapper.java:87) の仮想メソッド 'android.content.res.Resourcesを android.content.Context.getResources()' NULLオブジェクト参照 上を起動しようとしandroid.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity。ジャワ:551) com.example.andrewtakao.testapp.ListViewActivityでInstrumentation.java android.app.Instrumentation.newActivityでjava.lang.Class.newInstance(ネイティブメソッド) で(ListViewActivity.java:20) (。 :1067) でandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) でandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) android.app.ActivityThread.-wrap11(ActivityThreadました。 java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)android.app.ActivityThread.mainでandroid.os.Looper.loop(Looper.java:148) でandroid.os.Handler.dispatchMessage(Handler.java:102) (ActivityThread.java:5417) ででjava.lang.reflect.Method.invoke(ネイティブメソッド)

誰でも私が間違っていることを説明できますか?どうもありがとうございます。あなたがnullのコンテキストを渡していると私はそれがここだ疑い

答えて

1

例外の詳細:

List<String> android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions)); 
onCreateの内側にあなたのリストのインスタンスを移動し

List<String> android_versions; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list_view); 
    android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions)) 
    listview = (ListView)findViewById(R.id.listview); 

説明:ActivitygetResources()を使用して、暗黙的にひとりでにでContextあるActivity自体を指します。 onCreateContextと呼ばれる前にまだインスタンス化されていないので、Contextはnullになります。 onCreateの後にのみ、Contextと呼ばれます。

+0

素晴らしい!どうもありがとうございます。しかし、あなたもできます –

+0

すごい!どうもありがとうございます。ヌル・コンテキストがどこに渡されているのか説明できますか? –

+0

@AndrewTakaoあなたに説明があります。 ;) –

関連する問題