2012-01-19 43 views
0

我在這一行NullPointerException異常錯誤

btn10.setOnClickListener(this); 

這讓NullPointerException錯誤是整個Java

package com.james.helloandroid2; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 


//** activity 
public class HelloAndroid2 extends Activity implements OnClickListener,  ColorPickerDialog.OnColorChangedListener { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.pokertable); 



    //** imagebuttons 
    ImageView button10 = (ImageView) findViewById(R.id.button10); 
    button10.setOnClickListener(this); 


    View btn9 = (View) findViewById(R.id.button9); 
    //btn9.setOnClickListener(this); 
    View btn8 = (View) findViewById(R.id.button8); 
    //btn8.setOnClickListener(this); 
    View btn7 = (View) findViewById(R.id.button7); 
    //btn7.setOnClickListener(this); 
    View btn6 = (View) findViewById(R.id.button6); 
    //btn6.setOnClickListener(this); 
    View btn5 = (View) findViewById(R.id.button5); 
    //btn5.setOnClickListener(this); 
    View btn4 = (View) findViewById(R.id.button4); 
    //btn4.setOnClickListener(this); 
    View btn3 = (View) findViewById(R.id.button3); 
    //btn3.setOnClickListener(this); 
    View btn2 = (View) findViewById(R.id.button2); 
    //btn2.setOnClickListener(this); 
    View btn1 = (View) findViewById(R.id.button1);   
    //btn1.setOnClickListener(this); { 
} 
//** not in use yet. 
public void colorChanged(int color) { 
    // TODO Auto-generated method stub 

} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    ColorPickerDialog dialog = new ColorPickerDialog(this, null, 0); 
    dialog.show(); 

} 

} 

林不知道爲什麼,如果有人可以幫助。

也只是ps。我有一些東西被削減,所以他們免除我的代碼..更多的筆記或我正在等待實際使用的代碼。

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="clip_horizontal" 
    android:layout_weight="0.20" 
    android:background="@drawable/tablebackground" 
    android:gravity="top" > 


    <ImageView 
     android:id="@+id/button8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/seat5" 
     android:src="@drawable/shapebutton5" /> 



    <ImageView 
     android:id="@+id/button10" 
     android:layout_width="55dp" 
     android:layout_height="40dp" 
     android:layout_marginTop="5dp" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/shapebutton10" /> 



    <ImageView 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="31dp" 
     android:layout_toRightOf="@+id/button10" 
     android:onClick="onMyButtonClick" 
     android:src="@drawable/shapebutton1" /> 



    <ImageView 
     android:id="@+id/button7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="30dp" 
     android:layout_toLeftOf="@+id/button10" 
     android:src="@drawable/shapebutton9" /> 



    <ImageView 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button7" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/shapebutton6" /> 



    <ImageView 
     android:id="@+id/button6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button3" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/shapebutton4" /> 



    <ImageView 
     android:id="@+id/button5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button6" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="25dp" 
     android:src="@drawable/shapebutton3" /> 



    <ImageView 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button5" 
     android:layout_below="@+id/button3" 
     android:src="@drawable/shapebutton2" /> 



    <ImageView 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button5" 
     android:layout_alignBottom="@+id/button5" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="19dp" 
     android:src="@drawable/shapebutton7" /> 



    <ImageView 
     android:id="@+id/button9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button4" 
     android:layout_alignBottom="@+id/button4" 
     android:layout_alignLeft="@+id/button1" 
     android:src="@drawable/shapebutton8" /> 

</RelativeLayout> 



01-18 23:09:31.842: D/AndroidRuntime(810): Shutting down VM 
01-18 23:09:31.842: W/dalvikvm(810): threadid=1: thread exiting with uncaught exception  (group=0x40015560) 
01-18 23:09:31.852: E/AndroidRuntime(810): FATAL EXCEPTION: main 
01-18 23:09:31.852: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.james.helloandroid2/com.james.helloandroid2.HelloAndroid2}:  java.lang.NullPointerException 
01-18 23:09:31.852: E/AndroidRuntime(810): at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
01-18 23:09:31.852: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.os.Looper.loop(Looper.java:123) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-18 23:09:31.852: E/AndroidRuntime(810): at java.lang.reflect.Method.invokeNative(Native Method) 
01-18 23:09:31.852: E/AndroidRuntime(810): at java.lang.reflect.Method.invoke(Method.java:507) 
01-18 23:09:31.852: E/AndroidRuntime(810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-18 23:09:31.852: E/AndroidRuntime(810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-18 23:09:31.852: E/AndroidRuntime(810): at dalvik.system.NativeStart.main(Native Method) 
01-18 23:09:31.852: E/AndroidRuntime(810): Caused by: java.lang.NullPointerException 
01-18 23:09:31.852: E/AndroidRuntime(810): at com.james.helloandroid2.HelloAndroid2.onCreate(HelloAndroid2.java:23) 
01-18 23:09:31.852: E/AndroidRuntime(810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-18 23:09:31.852: E/AndroidRuntime(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
01-18 23:09:31.852: E/AndroidRuntime(810): ... 11 more 
+0

當你點擊按鈕時,你是否會收到零點異常?或者當你運行這段代碼時,你會得到異常? – Viking

+0

當我運行代碼。它強制關閉,當我打開它 – user1148316

回答

0

您是在R.layout.pokertable中定義您的ImageView嗎? Activity.findViewById不是神奇的,它只是沿着附加到活動當前窗口的視圖層次結構執行遞歸搜索。如果尚未使用setContentView附加這些視圖,則findViewById將返回null,因爲沒有匹配的視圖可用於查找。

也許發佈您的活動的XML佈局文件將幫助我們指出問題。

+0

我添加了佈局到OP – user1148316

+0

你不應該需要嵌套'LinearLayout'' RelativeLayout'。選擇一個,而不是兩個。在這種情況下,它看起來像你想完全擺脫'LinearLayout'。 –

+0

好吧我失去了linearlayout(我用它有一個遠處的背景後面的背景,雖然沒有大),但我仍然得到空指針 – user1148316

0
setContentView(R.layout.pokertable); 

是 「pokertable」 的佈局的XML文件名的名稱。如果你錯誤地設置了它,可能會導致這個異常。

+0

它是正確的佈局名稱 – user1148316

0

嘗試覆蓋onClick()method.Add @override on onClick on()方法的頂部。

+0

當我忽略它,它給了我一個錯誤刪除@override之前,我可以繼續 – user1148316

+0

我想你只能實現每一個class.try刪除監聽和ColorPickerDialog.OnColorChangedListener看它是否works.If它比你應該做單獨的類來處理對話框監聽器和調用此類中的對話框。 – Viking