在创建新的Intent对象时,我总是会遇到Nullpointer异常。 我的目的是创建一个新的Activity,它以“onCreate”方法打开一个新的View。
这是我的主要活动:
public class MainActivity extends Activity {
public void activateTimer(){
// some code ....
MyTimer timer = new MyTimer(MainActivity.this);
}
}
这是我的Timer类:
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);
}
}
应该创建我的活动:
public class MyNotificationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
}
}
以下是错误日志:
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)
这是我的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>
答案 0 :(得分:6)
您的MyTimer
延伸Activity
,但在此您自己实例化了这些内容:
MyTimer timer = new MyTimer(MainActivity.this)
现在,当您执行此操作时,活动未正确初始化以用作活动或上下文,并且可能出现各种类型的故障。
似乎您应该完全从extends Activity
中删除MyTimer
,并将Activity
或Context
参数传递给需要它的方法。例如,
public void openMyActivity(Activity act) {
// ...
act.startActivityForResult(...);
答案 1 :(得分:0)
为什么要将Mytimer扩展为MainActivity?我认为不需要那个
将其更改为....
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);
}
}