基本上我正在尝试运行一个简单的Android应用程序而且我做了所有事情都不正常但是当它在模拟器上启动时它总是向我显示这个错误消息可以解释一下这里的错误在哪里?
03-23 14:23:31.590: D/AndroidRuntime(611): Shutting down VM
03-23 14:23:31.620: W/dalvikvm(611): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-23 14:23:31.751: E/AndroidRuntime(611): FATAL EXCEPTION: main
03-23 14:23:31.751: E/AndroidRuntime(611): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.training.mcuisine/com.training.mcuisine.MainActivity}: java.lang.NullPointerException
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.os.Looper.loop(Looper.java:132)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread.main(ActivityThread.java:4025)
03-23 14:23:31.751: E/AndroidRuntime(611): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 14:23:31.751: E/AndroidRuntime(611): at java.lang.reflect.Method.invoke(Method.java:491)
03-23 14:23:31.751: E/AndroidRuntime(611): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-23 14:23:31.751: E/AndroidRuntime(611): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-23 14:23:31.751: E/AndroidRuntime(611): at dalvik.system.NativeStart.main(Native Method)
03-23 14:23:31.751: E/AndroidRuntime(611): Caused by: java.lang.NullPointerException
03-23 14:23:31.751: E/AndroidRuntime(611): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.content.ComponentName.<init>(ComponentName.java:75)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.content.Intent.<init>(Intent.java:2876)
03-23 14:23:31.751: E/AndroidRuntime(611): at com.training.mcuisine.MainActivity.<init>(MainActivity.java:14)
03-23 14:23:31.751: E/AndroidRuntime(611): at java.lang.Class.newInstanceImpl(Native Method)
03-23 14:23:31.751: E/AndroidRuntime(611): at java.lang.Class.newInstance(Class.java:1301)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
03-23 14:23:31.751: E/AndroidRuntime(611): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1663)
03-23 14:23:31.751: E/AndroidRuntime(611): ... 11 more
那是我的android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.training.mcuisine"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.training.mcuisine.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.training.mcuisine.jus"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
,这是MainActivity的活动代码
package com.training.mcuisine;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends Activity {
Intent jusIn = new Intent(MainActivity.this,jus.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton jus = (ImageButton) findViewById(R.id.jus);
jus.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startActivity(jusIn);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是唯一的其他简单分支我只保留内容视图只是为了知道问题是什么
包com.training.mcuisine;
import android.app.Activity;
import android.os.Bundle;
public class jus extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.jus);
}
}
答案 0 :(得分:1)
您需要更改
Intent jusIn = new Intent(MainActivity.this,jus.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
要
Intent jusIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jusIn = new Intent(MainActivity.this,jus.class);