Nullpointerexception error - PullRequest
       17

Nullpointerexception error

0 голосов
/ 19 января 2012

Я получаю сообщение об ошибке nullpointerexception в этой строке

btn10.setOnClickListener(this);

Это вся 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();

}

}

Я не уверен, почему, если кто-то может помочь.

также просто пс.у меня есть некоторые вещи, перерезанные в прямом направлении, так что они исключены в моем коде ... больше примечаний для меня или кода, который я жду, чтобы фактически использовать.

<?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

Ответы [ 3 ]

0 голосов
/ 19 января 2012

Вы определяете свои ImageView s в R.layout.pokertable?Activity.findViewById не магический, он просто выполняет рекурсивный поиск по иерархии представлений, прикрепленной к текущему окну активности.Если вы еще не прикрепили эти представления с помощью setContentView, findViewById вернет ноль, поскольку не было найдено подходящих представлений для поиска.

Возможно, размещение файла макета XML вашей деятельности поможет нам точно определить проблему.

0 голосов
/ 19 января 2012

попробуйте переопределить метод onClick (). Добавьте @override прямо поверх метода onClick ().

0 голосов
/ 19 января 2012
setContentView(R.layout.pokertable);

Является ли "pokertable" именем вашего макета имени файла XML. Если вы установите это неправильно, это может привести к этому исключению.

...