我正在开发一个简单的应用程序来了解Android,但每当我尝试运行它时,我都会收到此错误:
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之外填充变量导致了问题。谢谢所有回答的人。
答案 0 :(得分:0)
private final Button b = (Button)findViewById(R.id.button1);
private final TextView v = (TextView)findViewById(R.id.textView1);
您尝试在尚未创建的活动中查找视图
在findViewById()
setContentView()
答案 1 :(得分:0)
变化
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());
}
到
private int presses = 0;
private Button b;
private TextView v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
v = (TextView)findViewById(R.id.textView1);
b.setOnClickListener(new ButtonClickHandler());
}
答案 2 :(得分:0)
您尚未创建活动时创建视图:
这样做:
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 Button b;
private TextView v ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
v = (TextView)findViewById(R.id.textView1);
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.");
}
}
}
答案 3 :(得分:0)
创建全局按钮 顶部的声明
private Button b;
private TextView v;
oncreate()方法中的引用
b = (Button)findViewById(R.id.button1);
v = (TextView)findViewById(R.id.textView1);