I am trying to write Java code that creates a button click event that populates an edit text box with my name. Unfortunately I am getting runtime errors. I checked almost all related topics and questions on stackoverflow but couldn’t figure it out my problem. Appreciated for your helps.
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>
Error 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
Advertisement
Answer
Use fragment_main.xml
instead of activity_main.xml
for MainActivity layout because Button and EditText is in fragment_main
with id’s by which you are trying to access in MainActivity :
setContentView(R.layout.fragment_main);