我遵循谷歌上的开发者指南,并尝试创建一个应用程序,目前只有 1 个活动。它没有太多,只有很少的项目,而且这个类的功能几乎是空的,但是当我激活它时它会崩溃。清单是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.remoteswitch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
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.example.remoteswitch.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>
</application>
</manifest>
我的活动类文件是这样的:
package com.example.remoteswitch;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.remoteswitch.MESSAGE";
int maxTime = 90;
CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
TextView statText = (TextView)findViewById(R.id.statusText);
TextView timerText = (TextView)findViewById(R.id.timerText);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timebar.setEnabled(false);
timebar.setMax(maxTime);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void activateTimer(View view) {
// Do something in response to button
//Intent intent = new Intent(this, DisplayMessageActivity.class);
if (checkbox.isChecked())
timebar.setEnabled(true);
}
public void startBoiler(View view) {
// Do something in response to button
//Intent intent = new Intent(this, SecondActivity.class);
//intent.putExtra(EXTRA_MESSAGE,"Hi");
//startActivity(intent);
}
}
我的下一步是添加另一个活动,但我只是想看看它是否基本正常工作.. logcat 是这样的:
01-30 11:03:28.477: E/AndroidRuntime(1539): FATAL EXCEPTION: main
01-30 11:03:28.477: E/AndroidRuntime(1539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.remoteswitch/com.example.remoteswitch.MainActivity}: java.lang.NullPointerException
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.os.Looper.loop(Looper.java:137)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-30 11:03:28.477: E/AndroidRuntime(1539): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539): at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:03:28.477: E/AndroidRuntime(1539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-30 11:03:28.477: E/AndroidRuntime(1539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-30 11:03:28.477: E/AndroidRuntime(1539): at dalvik.system.NativeStart.main(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539): Caused by: java.lang.NullPointerException
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.Activity.findViewById(Activity.java:1839)
01-30 11:03:28.477: E/AndroidRuntime(1539): at com.example.remoteswitch.MainActivity.<init>(MainActivity.java:15)
01-30 11:03:28.477: E/AndroidRuntime(1539): at java.lang.Class.newInstanceImpl(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539): at java.lang.Class.newInstance(Class.java:1319)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-30 11:03:28.477: E/AndroidRuntime(1539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
谁能帮我找出我的错误?