2012-01-19 6 views
0

내가이 줄NullPointerException이 오류

btn10.setOnClickListener(this); 

이에 NullPointerException이 오류를 얻고 것은 누군가가 도움이 될 수 있다면 왜 전체 자바

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

ImageViewR.layout.pokertable에 정의 하시겠습니까? Activity.findViewById은 마법 같은 것이 아니며 활동의 현재 창에 첨부 된보기 계층 구조를 재귀 적으로 검색하기 만하면됩니다. setContentView을 사용하여 이러한보기를 연결하지 않은 경우 일치하는보기가 없으므로 findViewById은 null을 반환합니다.

아마도 액티비티의 XML 레이아웃 파일을 게시하면 문제를 정확히 파악하는 데 도움이됩니다.

+0

레이아웃에 OP – user1148316

+0

을 추가하면'RelativeLayout'을'LinearLayout'에 중첩 할 필요가 없습니다. 둘 중 하나만 선택하십시오. 이 경우에는'LinearLayout'을 완전히 없애고 싶은 것처럼 보입니다. –

+0

ok linearlayout을 잃어 버렸습니다. 즉각적인 배경 뒤에서 멀리있는 배경을 가지고 있었지만 큰 것은 아니지만 여전히 nullpointer를 얻었습니다. – user1148316

0
setContentView(R.layout.pokertable); 

는 레이아웃 XML 파일 이름의 "pokertable"이름입니다. 잘못 설정하면이 예외가 발생할 수 있습니다.

+0

올바른 레이아웃 이름입니다 – user1148316

0

onClick() 메소드를 재정 의하여 시도하십시오. onClick() 메소드 바로 위에 @override를 추가하십시오.

+0

내가 재정의 할 때, 나는 내가 계속하기 전에 @override를 제거하는 오류를 준다. – user1148316

+0

ColorPickerDialog.OnColorChangedListener를 제거한 class.try 당 하나의 Listener 만 구현할 수 있다고 생각하고 그것이 작동하는지 확인한다. 대화 상자 리스너를 처리하기 위해 별도의 클래스를 작성하고 이미 완료 한 방식으로이 클래스의 대화 상자를 호출하십시오. – Viking

관련 문제