こんにちは私は少しのAndroidアプリ(バージョン2.3.3)を書いています。今、私はこの非常に基本的なコードでこの奇妙にNullPointer例外を取得:findViewByIdを使用しているときにonCreate()でNullPointerExceptionが発生しました - 以前にsetContentViewが使用されていましたか?
メインmenu.xmlで、現時点では、このシンプルなレイアウトを使用してpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
newDeck = (Button) findViewById(R.id.newDeckB);
loadDeck = (Button) findViewById(R.id.loadDeckB);
viewEdition = (Button) findViewById(R.id.viewEditionB);
newDeck.setOnClickListener(this);
loadDeck.setOnClickListener(this);
viewEdition.setOnClickListener(this);
}
イム:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/newDeckB"
android:text="New Deck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/loadDeckB"
android:text="Load Deck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/viewEditionB"
android:text="View Edition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/currentDeckTextView"
android:text="Default Deck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
今私の問題は、ライン25でnullpointexceptionです、iはボタンnewDeckがヌルであることを考え出したデバッガを使用して第1 clickListener
newDeck.setOnClickListener(this);
を設定ラインはあります。私はウェブで多くを検索しましたが、この種の問題に対する唯一の答えは、setContentViewがfindViewByIdの前に設定されていることを確認することでした。これは明らかにここに当てはまります。
私はどんなアドバイスでも非常にうれしいです。
Thx in Before!
)(ONSTARTへのonCreate(から "findViewByID")を移動しなければならなかった 'R.id.newDeckB'は、実際にR.layout.mainmenu''に存在していますか? –
私はまったく同じ問題(イライラしていました!)を持っていました。それは私のために解決したコメント(@ manelizzard)でした: '時々、あなたは "きれいにする"必要があります。 Eclipseから。 (つまり、 'onPostCreate()'を使う必要はありません!) – coco
クリーンで再構築がうまくいかない。 –