Android application can not load once a new class is introduced -
hello there crawling android world , next android bootcamp exercise 2012 edition. able test application on avm 1 layout , 1 class on introducing sec class, sec layout , coding button, application not open , log cat error looks this
06-18 17:22:29.461: d/androidruntime(542): shutting downwards vm 06-18 17:22:29.461: w/dalvikvm(542): threadid=1: thread exiting uncaught exception (group=0x409961f8) 06-18 17:22:29.471: e/androidruntime(542): fatal exception: main 06-18 17:22:29.471: e/androidruntime(542): java.lang.runtimeexception: unable start activity componentinfo{com.example.helloworld/com.example.helloworld.main}: java.lang.nullpointerexception 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread.performlaunchactivity(activitythread.java:1955) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1980) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread.access$600(activitythread.java:122) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread$h.handlemessage(activitythread.java:1146) 06-18 17:22:29.471: e/androidruntime(542): @ android.os.handler.dispatchmessage(handler.java:99) 06-18 17:22:29.471: e/androidruntime(542): @ android.os.looper.loop(looper.java:137) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread.main(activitythread.java:4340) 06-18 17:22:29.471: e/androidruntime(542): @ java.lang.reflect.method.invokenative(native method) 06-18 17:22:29.471: e/androidruntime(542): @ java.lang.reflect.method.invoke(method.java:511) 06-18 17:22:29.471: e/androidruntime(542): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 06-18 17:22:29.471: e/androidruntime(542): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 06-18 17:22:29.471: e/androidruntime(542): @ dalvik.system.nativestart.main(native method) 06-18 17:22:29.471: e/androidruntime(542): caused by: java.lang.nullpointerexception 06-18 17:22:29.471: e/androidruntime(542): @ com.example.helloworld.main.oncreate(main.java:24) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activity.performcreate(activity.java:4465) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 06-18 17:22:29.471: e/androidruntime(542): @ android.app.activitythread.performlaunchactivity(activitythread.java:1919) 06-18 17:22:29.471: e/androidruntime(542): ... 11 more this main.java
public class main extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button b = (button)findviewbyid(r.id.btnplay); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { startactivity(new intent(main.this, recipe.class)); // todo auto-generated method stub } }); if (savedinstancestate == null) { getfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming 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) { homecoming true; } homecoming 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); homecoming rootview; } } } this new class
package com.example.helloworld; import android.app.activity; import android.os.bundle; public class recipe extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.recipe); } } this manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="14" 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.helloworld.main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="recipe"></activity> </application> </manifest> activity_main
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.helloworld.main" tools:ignore="mergerootframe" /> fragment_main
<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.helloworld.main$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="28dp" android:text="@string/mduduzi_games" android:textsize="30sp" /> <button android:id="@+id/btnplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageview1" android:layout_centerhorizontal="true" android:layout_margintop="28dp" android:text="@string/play" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerhorizontal="true" android:layout_margintop="27dp" android:src="@drawable/anim4" /> </relativelayout>
from stack trace, there nullpointerexception on line 24 of main.java:
at com.example.helloworld.main.oncreate(main.java:24) this problem:
button b = (button)findviewbyid(r.id.btnplay); //b null, next line throws nullpointerexception: b.setonclicklistener(... b null, meaning there no button id "btnplay" in activity_main.xml
check create sure have in activity_main.xml:
<button android:layout_width="wrap_content" android:text="some text" android:id="@+id/btnplay" android:layout_height="wrap_content" /> edit: you calling findviewbyid activity, searching amongst views of activity defined activity_main.xml. since have defined button in fragment layout, must reference button within fragment code. remove button code activity class, , stick fragment:
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); //find button view in rootview button b = (button)rootview.findviewbyid(r.id.btnplay); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { startactivity(new intent(main.this, recipe.class)); // todo auto-generated method stub } }); homecoming rootview; } } android
No comments:
Post a Comment