由Java.lang.nullpointerexception引起

时间:2014-10-26 14:56:49

标签: java android nullpointerexception oncreate

我找到了findViewById'和' setOnClickListener'在声明' OnCreate()'之后,它无法运行时出现一些错误。这是我的代码的某些部分。

    import android.app.Activity;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.wearable.view.WatchViewStub;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MyActivity extends Activity {

        private TextView mTextView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);

            //getnextpage
            Button getnextpage;
            getnextpage = (Button) findViewById(R.id.getnextpage);
            getnextpage.setOnClickListener(new OnclickListener() {
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Here is 2nd Page",
                            Toast.LENGTH_LONG).show();
                }
            });

     final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
          }
      });

    }
 }

这里的错误代码。它说错误是由nullpointerexception引起的

Caused by: java.lang.NullPointerException
at com.example.sparkym.androidwear.MyActivity.onCreate(MyActivity.java:32)   
at android.app.Activity.performCreate(Activity.java:5242)    
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)          
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)     
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)      
at android.app.ActivityThread.access$800(ActivityThread.java:138)      
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)         
at android.os.Handler.dispatchMessage(Handler.java:102)      
at android.os.Looper.loop(Looper.java:136)        
at android.app.ActivityThread.main(ActivityThread.java:5026)       
at java.lang.reflect.Method.invokeNative(Native Method)          
at java.lang.reflect.Method.invoke(Method.java:515)     
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)        
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)    
at dalvik.system.NativeStart.main(Native Method)       

0 个答案:

没有答案