在 Activity 中访问小部件时出现 NullPointerException
Posted
技术标签:
【中文标题】在 Activity 中访问小部件时出现 NullPointerException【英文标题】:NullPointerException when accessing the widget in Activity 【发布时间】:2013-10-16 03:57:04 【问题描述】:我的 LogCat 显示 NullPointerException
,无论我将哪个代码作为主要活动运行。我在相同或不同项目中尝试的所有活动都发生了相同的异常。
MainActivity
public class MainActivity extends Activity implements View.OnClickListener
private static EditText et;
private static Button b;
private static TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
TextView tv=(TextView)findViewById(R.id.textView1);
b.setOnClickListener(this);
setContentView(R.layout.activity_main);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
tv.setText(et.getText());
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="TextView" />
</RelativeLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.stuff"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.stuff.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>
LogCat
10-09 05:52:58.953: E/Trace(667): error opening trace file: No such file or directory (2)
10-09 05:53:00.152: D/AndroidRuntime(667): Shutting down VM
10-09 05:53:00.172: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-09 05:53:00.202: E/AndroidRuntime(667): FATAL EXCEPTION: main
10-09 05:53:00.202: E/AndroidRuntime(667): java.lang.RuntimeException: Unable to start activity ComponentInfocom.android.stuff/com.android.stuff.MainActivity: java.lang.NullPointerException
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.os.Looper.loop(Looper.java:137)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-09 05:53:00.202: E/AndroidRuntime(667): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 05:53:00.202: E/AndroidRuntime(667): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 05:53:00.202: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-09 05:53:00.202: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 05:53:00.202: E/AndroidRuntime(667): at dalvik.system.NativeStart.main(Native Method)
10-09 05:53:00.202: E/AndroidRuntime(667): Caused by: java.lang.NullPointerException
10-09 05:53:00.202: E/AndroidRuntime(667): at com.android.stuff.MainActivity.onCreate(MainActivity.java:23)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.Activity.performCreate(Activity.java:5008)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-09 05:53:00.202: E/AndroidRuntime(667): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-09 05:53:00.202: E/AndroidRuntime(667): ... 11 more
10-09 05:53:03.652: I/Process(667): Sending signal. PID: 667 SIG: 9
【问题讨论】:
把 setContentView(R.layout.activity_main);在 super.onCreate(savedInstanceState); 之后 请验证答案,以感谢花时间回答您问题的人。在此先感谢您。 【参考方案1】:在访问组件之前设置内容视图。另外,不要在onCreate()
中再次声明该类型,因为它们将成为覆盖静态变量的局部变量。
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
b=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.textView1);
b.setOnClickListener(this);
【讨论】:
【参考方案2】:在你的 onCreate 方法中使用它:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
【讨论】:
以上是关于在 Activity 中访问小部件时出现 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
从另一个 Activity 访问一个 Activity 中的小部件
Android:小部件 - 在远程视图上使用 setOnclickPendingIntent 时出现空指针异常?