java - Eclipse: "Unfortunately, XXX has stopped" error when testing on an Android phone -
please forgive me, since first time make android program.
so i've followed tutorial on youtube make simple program take user inputted number, multiply 5 , display on label.
it got no errors. when tried run on android phone, got error "unfortunately, xxx has stopped". i've checked , run hello world program eclipse initiated program with.
thank kind attention.
mainactivity.java
package com.example.converter; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.*; // * wild character, import widgets import android.os.build; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } button calc = (button)findviewbyid(r.id.btncalculate); calc.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { edittext num = (edittext)findviewbyid(r.id.num); textview display = (textview)findviewbyid(r.id.display); double number = double.parsedouble(num.gettext().tostring()); number = number * 5; display.settext(number + ""); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } }
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.converter.mainactivity$placeholderfragment" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="51dp" android:text="@string/basic_calc" android:textappearance="?android:attr/textappearancelarge" /> <edittext android:id="@+id/num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerhorizontal="true" android:ems="10" android:inputtype="numbersigned|numberdecimal" > <requestfocus /> </edittext> <button android:id="@+id/btncalculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/num" android:layout_centerhorizontal="true" android:layout_margintop="18dp" android:text="@string/calculate" /> <textview android:id="@+id/display" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/num" android:layout_below="@+id/btncalculate" android:layout_margintop="60dp" android:textappearance="?android:attr/textappearancelarge" /> </relativelayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.converter" 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.converter.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-24 22:34:24.665: e/trace(18634): error opening trace file: no such file or directory (2) 03-24 22:34:24.845: w/dalvikvm(18634): threadid=1: thread exiting uncaught exception (group=0x413b3390) 03-24 22:34:24.865: e/androidruntime(18634): fatal exception: main 03-24 22:34:24.865: e/androidruntime(18634): java.lang.runtimeexception: unable start activity componentinfo{com.example.converter/com.example.converter.mainactivity}: java.lang.nullpointerexception 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread.performlaunchactivity(activitythread.java:2355) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2391) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread.access$600(activitythread.java:151) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread$h.handlemessage(activitythread.java:1335) 03-24 22:34:24.865: e/androidruntime(18634): @ android.os.handler.dispatchmessage(handler.java:99) 03-24 22:34:24.865: e/androidruntime(18634): @ android.os.looper.loop(looper.java:155) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread.main(activitythread.java:5511) 03-24 22:34:24.865: e/androidruntime(18634): @ java.lang.reflect.method.invokenative(native method) 03-24 22:34:24.865: e/androidruntime(18634): @ java.lang.reflect.method.invoke(method.java:511) 03-24 22:34:24.865: e/androidruntime(18634): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1029) 03-24 22:34:24.865: e/androidruntime(18634): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:796) 03-24 22:34:24.865: e/androidruntime(18634): @ dalvik.system.nativestart.main(native method) 03-24 22:34:24.865: e/androidruntime(18634): caused by: java.lang.nullpointerexception 03-24 22:34:24.865: e/androidruntime(18634): @ com.example.converter.mainactivity.oncreate(mainactivity.java:28) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activity.performcreate(activity.java:5066) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1101) 03-24 22:34:24.865: e/androidruntime(18634): @ android.app.activitythread.performlaunchactivity(activitythread.java:2311) 03-24 22:34:24.865: e/androidruntime(18634): ... 11 more 03-24 22:34:26.517: d/process(18634): killprocess, pid=18634 03-24 22:34:26.527: d/process(18634): dalvik.system.vmstack.getthreadstacktrace(native method) 03-24 22:34:26.527: d/process(18634): java.lang.thread.getstacktrace(thread.java:599) 03-24 22:34:26.527: d/process(18634): android.os.process.killprocess(process.java:951) 03-24 22:34:26.527: d/process(18634): com.android.internal.os.runtimeinit$uncaughthandler.uncaughtexception(runtimeinit.java:108) 03-24 22:34:26.527: d/process(18634): java.lang.threadgroup.uncaughtexception(threadgroup.java:693) 03-24 22:34:26.527: d/process(18634): java.lang.threadgroup.uncaughtexception(threadgroup.java:690) 03-24 22:34:26.527: d/process(18634): dalvik.system.nativestart.main(native method)
as eltabo's answer indicates: calc null, because r.id.btncalculate has been declared in fragment_main.xml, you're trying find in mainactivity loads layout activity_main.xml.
so solution move fragment so:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main, container, false); button calc = (button) view.findviewbyid(r.id.btncalculate); final edittext num = (edittext) view.findviewbyid(r.id.num); final textview display = (textview) view.findviewbyid(r.id.display); calc.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { double number = double.parsedouble(num.gettext().tostring()); number = number * 5; display.settext(number + ""); } }); return view; }
Comments
Post a Comment