Android:将主类扩展到Actionbar活动时发生错误(java.lang.RuntimeException:无法启动活动组件信息)
我在主类中编写了代码,将其扩展到活动类,因为我的最小sdk级别为8,所以我无法使用actionbar,因此在编写所有代码后,我将扩展类更改为ActionBarActivity类替换活动类。除了导入ActionBarActivity类之外,我没有收到任何错误。但是我在运行时出错了。请帮我解决这个问题 MainActivity.javaAndroid:将主类扩展到Actionbar活动时发生错误(java.lang.RuntimeException:无法启动活动组件信息),android,nullpointerexception,runtimeexception,Android,Nullpointerexception,Runtimeexception,我在主类中编写了代码,将其扩展到活动类,因为我的最小sdk级别为8,所以我无法使用actionbar,因此在编写所有代码后,我将扩展类更改为ActionBarActivity类替换活动类。除了导入ActionBarActivity类之外,我没有收到任何错误。但是我在运行时出错了。请帮我解决这个问题 MainActivity.java package com.vibrator; import android.annotation.SuppressLint; import android.os.B
package com.vibrator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
@SuppressLint("ShowToast")
public class MainActivity extends ActionBarActivity
implements OnCheckedChangeListener, OnSeekBarChangeListener{
ToggleButton tbutton;
Vibrator v;
int i;
int j;
TextView txt;
SeekBar sb;
TextView txt1;
SeekBar sb1;
protected void onCreate(Bundle savedInstanceState) {
//creating instance for vibrator
v=(Vibrator) getSystemService(VIBRATOR_SERVICE);
//checking if the device has vibrator or not
if(v!=null){
setContentView(R.layout.activity_main);
}
else {
setContentView(R.layout.secound);
}
//Log.e("vibrate", "setcontentview");
//creating instance of toggle buttton and setting listener
tbutton=(ToggleButton) findViewById(R.id.tb1);
tbutton.setOnCheckedChangeListener(this);
super.onCreate(savedInstanceState);
}
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
Log.d("real status", ""+arg1);
//if vibrator is ON
if(arg1)
{
Log.d("vibrate", "if loop");
Toast.makeText(this,"status: "+arg1, Toast.LENGTH_LONG).show();
//creating instance of seekbar and textview
sb=(SeekBar) findViewById(R.id.seekbar);
txt=(TextView) findViewById(R.id.txt12);
sb1=(SeekBar) findViewById(R.id.seekbar1);
txt1=(TextView) findViewById(R.id.txt11);
//changing visibility
txt.setVisibility(View.VISIBLE);
sb.setVisibility(View.VISIBLE);
txt1.setVisibility(View.VISIBLE);
sb1.setVisibility(View.VISIBLE);
sb1.setOnSeekBarChangeListener(this);
sb.setOnSeekBarChangeListener(this);
// Start without a delay
// Vibrate for 100 j milliseconds
// Sleep for 1000 (i) milliseconds
long[] pattern = {0, j, i};
// The '0' here means to repeat indefinitely
// '-1' would play the vibration once
v.vibrate(pattern, 0);
}
else {
sb.setVisibility(View.INVISIBLE);
txt.setVisibility(View.INVISIBLE);
sb1.setVisibility(View.INVISIBLE);
txt1.setVisibility(View.INVISIBLE);
Log.d("vibrate", "else loop");
Toast.makeText(this,"status: "+arg1, Toast.LENGTH_LONG).show();
v.cancel();
}
}
@Override
public void onProgressChanged(SeekBar s, int v, boolean b) {
switch (s.getId()) {
case R.id.seekbar:
i=v;
break;
case R.id.seekbar1:
j=v;
break;
}
txt.setText("Vibrator OFF time: "+i);
txt1.setText("Vibrator ON time: "+j);
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
}
日志:
07-28 01:10:41.306: D/AndroidRuntime(381): Shutting down VM
07-28 01:10:41.306: W/dalvikvm(381): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-28 01:10:41.326: E/AndroidRuntime(381): FATAL EXCEPTION: main
07-28 01:10:41.326: E/AndroidRuntime(381): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vibrator/com.vibrator.MainActivity}: java.lang.NullPointerException
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.os.Looper.loop(Looper.java:123)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 01:10:41.326: E/AndroidRuntime(381): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 01:10:41.326: E/AndroidRuntime(381): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 01:10:41.326: E/AndroidRuntime(381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 01:10:41.326: E/AndroidRuntime(381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 01:10:41.326: E/AndroidRuntime(381): at dalvik.system.NativeStart.main(Native Method)
07-28 01:10:41.326: E/AndroidRuntime(381): Caused by: java.lang.NullPointerException
07-28 01:10:41.326: E/AndroidRuntime(381): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
07-28 01:10:41.326: E/AndroidRuntime(381): at com.vibrator.MainActivity.onCreate(MainActivity.java:37)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 01:10:41.326: E/AndroidRuntime(381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 01:10:41.326: E/AndroidRuntime(381): ... 11 more
07-28 01:15:41.356: I/Process(381): Sending signal. PID: 381 SIG: 9
您将获得
NullPointerException
,因为ActionBarActivity
要求在设置内容视图之前调用super.onCreate(savedInstanceState)
方法
尝试将super.onCreate(savedInstanceState)
移动到onCreate
方法的第一行
希望有帮助