我正在尝试编写创建按钮单击事件的Java代码,该事件使用我的名称填充编辑文本框。不幸的是我遇到运行时错误。我检查了几乎所有关于stackoverflow的相关主题和问题,但无法弄清楚我的问题。 感谢您的帮助。
MainActivity.java
package com.example.question5;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
Button b;
EditText et;
@Override
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);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et.setText("My Name");
}
});
}
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.question5.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="178dp"
android:text="Populate" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.question5"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.question5.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
03-21 23:38:26.227: W/dalvikvm(28343): threadid=1: thread exiting with uncaught exception (group=0x40bfd1f8)
03-21 23:38:26.227: E/AndroidRuntime(28343): FATAL EXCEPTION: main
03-21 23:38:26.227: E/AndroidRuntime(28343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.question5/com.example.question5.MainActivity}: java.lang.NullPointerException
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.os.Looper.loop(Looper.java:137)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread.main(ActivityThread.java:4514)
03-21 23:38:26.227: E/AndroidRuntime(28343): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 23:38:26.227: E/AndroidRuntime(28343): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 23:38:26.227: E/AndroidRuntime(28343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-21 23:38:26.227: E/AndroidRuntime(28343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-21 23:38:26.227: E/AndroidRuntime(28343): at dalvik.system.NativeStart.main(Native Method)
03-21 23:38:26.227: E/AndroidRuntime(28343): Caused by: java.lang.NullPointerException
03-21 23:38:26.227: E/AndroidRuntime(28343): at com.example.question5.MainActivity.onCreate(MainActivity.java:19)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.Activity.performCreate(Activity.java:4465)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
03-21 23:38:26.227: E/AndroidRuntime(28343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
03-21 23:38:26.227: E/AndroidRuntime(28343): ... 11 more
答案 0 :(得分:2)
使用fragment_main.xml
代替activity_main.xml
进行MainActivity布局,因为Button和EditText位于fragment_main
,其中包含您尝试在MainActivity中访问的ID:
setContentView(R.layout.fragment_main);