Activity中的java.lang.NullPointerException

时间:2014-04-25 10:20:17

标签: android view nullpointerexception

当我为显示setOnLongClickListener的视图设置nullpointerexception时。在我的代码中 erro进入totaltime.setOnLongClickListenerdailytime.setOnLonclicklistener等等。

以下是我的代码。

public class PrincipalActivity extends Activity {   
    DefaultSpeedoView totaltime;
    DefaultSpeedoView1 dailytime;
    DefaultSpeedoView4 loadcount;
    DefaultSpeedoView5 elapsetime;
    DefaultSpeedoView6 yearlytime;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mdb = new MyDataBase(this);
        setContentView(R.layout.principal);
        editornot=(TextView) findViewById(R.id.edited);


        totaltime=(DefaultSpeedoView) findViewById(R.id.totaltime);
        dailytime=(DefaultSpeedoView1) findViewById(R.id.dailytime);
        yearlytime=(DefaultSpeedoView6) findViewById(R.id.yearlytime);
        loadcount=(DefaultSpeedoView4) findViewById(R.id.loadcount);
        elapsetime=(DefaultSpeedoView5) findViewById(R.id.elapsetime);
        editmodecolorchange=(RelativeLayout)findViewById(R.id.editmodecolorchange);
        totaltime.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                alertDialog("Total Time");
                return false;
            }
        });
        dailytime.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                alertDialog("Daily Time");
                return false;
            }
        });
        yearlytime.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                alertDialog("Yearly Time");
                return false;
            }
        });
        loadcount.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                alertDialog1("Load Count");
                return false;
            }
        });
        elapsetime.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                alertDialog1("Elapse Time");
                return false;
            }
        });
}

DefaultSpeedoView.Java

public DefaultSpeedoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.con = context;
        // TODO Auto-generated constructor stub
         init();

    }

    public DefaultSpeedoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.con = context;
        init();
    }

    public DefaultSpeedoView(Context context) {
        super(context);

        this.con = context;
        init();

    }

XML:

      <com.appp.Timer.DefaultSpeedoView
            android:id="@+id/totaltime"
            android:layout_width="250dp"
            android:layout_height="215dp"
            android:longClickable="true"
            android:layout_marginLeft="50dp" />

日志:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.shipp.activity/org.shipp.activity.PrincipalActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.shipp.activity.PrincipalActivity.onCreate(PrincipalActivity.java:179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

1 个答案:

答案 0 :(得分:0)

    dailytime=(DefaultSpeedoView1) findViewById(R.id.dailytime);
    yearlytime=(DefaultSpeedoView6) findViewById(R.id.yearlytime);
    loadcount=(DefaultSpeedoView4) findViewById(R.id.loadcount);
    elapsetime=(DefaultSpeedoView5) findViewById(R.id.elapsetime);
    editmodecolorchange=(RelativeLayout)findViewById(R.id.editmodecolorchange);

您应该在xml文件中正确指定它们,如

   <com.appp.Timer.DefaultSpeedoView
        android:id="@+id/dailytime"
        android:layout_width="250dp"
        android:layout_height="215dp"
        android:longClickable="true"
        android:layout_marginLeft="50dp" />

并正确地将其投放到DefaultSpeedoView(不是DefaultSpeedoView1DefaultSpeedoView2等等)