获取错误致命java.lang.RuntimeException:无法启动活动
我是android编程新手,只懂一点java背景。出于某种原因,基于新的Boston Android教程系列制作的程序中的3个类会产生类似以下错误:获取错误致命java.lang.RuntimeException:无法启动活动,java,android,runtime-error,Java,Android,Runtime Error,我是android编程新手,只懂一点java背景。出于某种原因,基于新的Boston Android教程系列制作的程序中的3个类会产生类似以下错误: 03-13 18:17:22.095: E/AndroidRuntime(13055): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infitenothing.dogcat/com.infitenothing.word.OpenClass}: jav
03-13 18:17:22.095: E/AndroidRuntime(13055): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infitenothing.dogcat/com.infitenothing.word.OpenClass}: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.os.Looper.loop(Looper.java:136)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 18:17:22.095: E/AndroidRuntime(13055): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055): at java.lang.reflect.Method.invoke(Method.java:515)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 18:17:22.095: E/AndroidRuntime(13055): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-13 18:17:22.095: E/AndroidRuntime(13055): at dalvik.system.NativeStart.main(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055): Caused by: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.infitenothing.word.OpenClass.intalize(OpenClass.java:43)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.infitenothing.word.OpenClass.onCreate(OpenClass.java:29)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.Activity.performCreate(Activity.java:5231)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-13 18:17:22.095: E/AndroidRuntime(13055): ... 12 more
我和这个班一起开展活动
public class Menu extends ListActivity {
String classes[] = {"MainActivity", "Splash", "TextPlay", "Email", "Camera", "DatA", "OpenClass", "example1", "example1"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes ));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.infitenothing.word." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
及
}
最后
package com.infitenothing.word;
import com.infitenothing.dogcat.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class OpenClass extends Activity implements OnClickListener,
OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectList;
String gotBread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
intalize();
/*
* Bundle gotBasket = getIntent().getExtras(); gotBread =
* gotBasket.getString("key"); question.setText(gotBread);
*/
}
private void intalize() {
// TODO Auto-generated method stub
question = (TextView) findViewById(R.id.tvQestion);
question = (TextView) findViewById(R.id.tvQestion);
selectList = (RadioGroup) findViewById(R.id.rgAnswer);
selectList.setOnCheckedChangeListener(this);
returnData.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// check id ma not be right vabable
switch (checkedId) {
case R.id.rcrazy:
break;
case R.id.rawsome:
break;
case R.id.rBoth:
break;
}
}
}
我的android清单是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.infitenothing.dogcat"
android:versionCode="1"
android:versionName="1.0009" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission android:name="android.permission.SET_WALLPAPER"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.infitenothing.word.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.infitenothing.word.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Menu"
android:label="SPLASH" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Splash"
android:label="SPLASH" >
<intent-filter>
<action android:name="com.infitenothing.word.Splash" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Magic"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.infitenothing.word.Magic" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.infitenothing.word.Email"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.infitenothing.word.Camera"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name="com.infitenothing.word.Data"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="com.infitenothing.word.Data" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.OpenClass"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
</activity>
</application>
</manifest>
抱歉,这篇文章太长了,但我已经四处寻找了一段时间,并没有找到任何好的答案来解决这个问题
解决此问题的任何答案都将是一个巨大的帮助您正在调用按钮上的setOnClickListener方法:
returnData.setOnClickListener(this);
但您以前从未初始化过此按钮!==>NPE
要解决此问题,您需要像使用其他视图一样使用findViewById:
returnData = (Button) findViewById(R.id.the_id_of_your_button);
returnData.setOnClickListener(this);
returnData = (Button) findViewById(R.id.the_id_of_your_button);