我工作的一個簡單的應用程序,以瞭解Android,但每當我嘗試運行它,我得到這個錯誤:Android應用程序將關閉,並無法實例活動ComponentInfo
11-28 14:51:34.536: E/AndroidRuntime(272): FATAL EXCEPTION: main
11-28 14:51:34.536: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.mdpr.wk1/hu.mdpr.wk1.MainActivity}: java.lang.NullPointerException
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.os.Looper.loop(Looper.java:123)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-28 14:51:34.536: E/AndroidRuntime(272): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 14:51:34.536: E/AndroidRuntime(272): at java.lang.reflect.Method.invoke(Method.java:521)
11-28 14:51:34.536: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-28 14:51:34.536: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-28 14:51:34.536: E/AndroidRuntime(272): at dalvik.system.NativeStart.main(Native Method)
11-28 14:51:34.536: E/AndroidRuntime(272): Caused by: java.lang.NullPointerException
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.Activity.findViewById(Activity.java:1637)
11-28 14:51:34.536: E/AndroidRuntime(272): at hu.mdpr.wk1.MainActivity.<init>(MainActivity.java:12)
11-28 14:51:34.536: E/AndroidRuntime(272): at java.lang.Class.newInstanceImpl(Native Method)
11-28 14:51:34.536: E/AndroidRuntime(272): at java.lang.Class.newInstance(Class.java:1429)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-28 14:51:34.536: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-28 14:51:34.536: E/AndroidRuntime(272): ... 11 more
根據大多數線程在這個問題上,這通常意味着我的Manifest存在問題,但我一直無法找到任何東西。
我的清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hu.mdpr.wk1"
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=".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 hu.mdpr.wk1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private int presses = 0;
private final Button b = (Button)findViewById(R.id.button1);
private final TextView v = (TextView)findViewById(R.id.textView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b.setOnClickListener(new ButtonClickHandler());
}
@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;
}
public class ButtonClickHandler implements View.OnClickListener
{
@Override
public void onClick(View source)
{
presses++;
v.setText("button pressed "+presses+" times.");
}
}
}
我已經試圖改變出口秩序,淨化工程和更改API級別,其中沒有固定的問題。請幫幫我。
編輯:事實證明,使按鈕最終,並試圖填充變量以外的onCreate導致的問題。感謝所有回答的人。