0

行ごとに 2 つの TextView を表示する ListView を作成しようとしています。

ただし、アクティビティを開くときに NullPointerException が発生し続け、その理由がわかりません (logCat ログでは、私の側のどの行が問題を引き起こしているのかわかりません)。

関連するコードは次のとおりです。

メイン クラス EventViewActivity

public class EventViewActivity extends ListActivity {
public String[] eventTitles;

@Override
public void onCreate(Bundle savedInstanceState) {
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.eventview_layout);

    eventTitles = new String[]{                 
            getResources().getString(R.string.eventtitle1),
            getResources().getString(R.string.eventtitle2),
            getResources().getString(R.string.eventtitle3),
            getResources().getString(R.string.eventtitle4),
            getResources().getString(R.string.eventtitle5)
    };

    setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
    setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}

layout.xml は次のとおりです。

 <LinearLayout xmlns:android="schemas.android.com/apk/res/android"
       android:layout_width="fill_parent" android:layout_height="wrap_content"
       android:orientation="vertical" >
       <ListView android:id="@+id/android:eventlist" android:layout_width="fill_parent"
        android:layout_height="fill_parent"
       android:background="@drawable/eventview_background"/>
</LinearLayout>

ありがとう!

4

3 に答える 3

0

次を呼び出す必要があります。

super.onCreate();

最初!

于 2012-04-04T08:22:48.690 に答える
0

ListView にアプローチしている方法は正しくないと思います。

このサンプルLINKを見て、カスタム アダプターを使用して ListView を作成する方法を完全に確認してください。

上記のリンクには、リストビューで作成されたサンプル アプリケーションがあり、各リスト項目には要件として 2 つのテキスト フィールドがあります。

于 2012-04-04T06:54:02.727 に答える
0

ID ..eventview_layoutの ListView がありますか? android:id="@android:id/list"それを追加しない場合..私はそれが行われていないと仮定します

ちょっと訂正。。

    <LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 
    <ListView android:id="@android:id/list"
 android:layout_width="fill_parent" 
android:layout_height="fill_parent"
 android:background="@drawable/eventview_background"/>
 </LinearLayout>

ListACtivity の contentView には、まったく同じ ID の listView が必要です。

于 2012-04-04T06:32:23.160 に答える