简体   繁体   English

Android:NullPointerException 创建新的意图

[英]Android: NullPointerException creating new Intent

I always get a Nullpointer Exception when creating a new Intent Object.创建新的 Intent 对象时,我总是会遇到 Nullpointer Exception。 My intention is to create a new Activity which opens a new View with its "onCreate" method.我的目的是创建一个新的 Activity,它用它的“onCreate”方法打开一个新的视图。

This is my Main Activity:这是我的主要活动:

public class MainActivity extends Activity {
    public void activateTimer(){
        // some code ....           
        MyTimer timer = new MyTimer(MainActivity.this);
    }
}

This is my Timer Class:这是我的计时器类:

public class MyTimer extends MainActivity{
    private Context contextMain;
    public MyTimer(Context context){
        this.contextMain = context;
        // some more code ...
        openMyActivity(contextMain); 
}
public void openMyActivity(Context contextMain){
    // some code ...
    // The next line throws the NPE!
Intent intent = new Intent(contextMain, MyNotificationActivity.class);
this.startActivity(intent);
    }
}

My Activity which should be created:我应该创建的活动:

public class MyNotificationActivity extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);
 } 
}

Here is the error log:这是错误日志:

03-21 12:47:51.949: W/dalvikvm(2442): threadid=11: thread exiting with uncaught exception (group=0x41465700)
03-21 12:47:51.959: E/AndroidRuntime(2442): FATAL EXCEPTION: Timer-0
03-21 12:47:51.959: E/AndroidRuntime(2442): java.lang.NullPointerException
03-21 12:47:51.959: E/AndroidRuntime(2442):     at android.app.Activity.startActivityForResult(Activity.java:3390)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at android.app.Activity.startActivityForResult(Activity.java:3351)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at android.app.Activity.startActivity(Activity.java:3587)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at android.app.Activity.startActivity(Activity.java:3555)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at com.myApp.MyTimer.openNotificationInMain(MyTimer.java:135)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at com.myApp.MyTimer.prompt(MyTimer.java:80)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at com.myApp.MyTimer$1.run(MyTimer.java:38)
03-21 12:47:51.959: E/AndroidRuntime(2442):     at java.util.Timer$TimerImpl.run(Timer.java:284)

This is my Manifest.xml File:这是我的 Manifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myApp"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.myApp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <activity android:name="com.myApp.MyNotifikationActivity"
              android:label="@string/app_name"
              android:parentActivityName="com.myApp.MainActivity"  >
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.myApp.MainActivity" />            
    </activity>         
</application>
</manifest>

Your MyTimer extends Activity but here you're instantiating it yourself:您的MyTimer扩展了Activity但在这里您自己实例化它:

MyTimer timer = new MyTimer(MainActivity.this)

Now, when you do this, the activity isn't properly initialized to be used as an activity or a context and all kinds of failures are possible.现在,当您执行此操作时,活动未正确初始化以用作活动或上下文,并且可能出现各种故障。

Seems like you should remove the extends Activity from MyTimer altogether and pass in an Activity or Context argument to those methods that need it.似乎您应该完全从MyTimer删除extends Activity并将ActivityContext参数传递给需要它的方法。 For example,例如,

public void openMyActivity(Activity act) {
  // ...
  act.startActivityForResult(...);

Why do you extend Mytimer to MainActivity?.为什么要将 Mytimer 扩展到 MainActivity? I don't see the need of that我不认为有必要

change it to ....将其更改为....

public class MyTimer{
private Context contextMain;
   public MyTimer(Context context){
    this.contextMain = context;
    // some more code ...
    openMyActivity(contextMain); 
  }
 public void openMyActivity(Context contextMain){
// some code ...
// The next line throws the NPE!
Intent intent = new Intent(contextMain, MyNotificationActivity.class);
this.startActivity(intent);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM