java - Null Pointer Exception -
i'm going nuts trying figure 1 out.when click on activity crashes nullpointerexception.i've tried debugging can't activity in question.i've tried removing database,still crashed.i tried removing listview,still crashed.i tried modifying database,still crashed.
for odd reason when removed button , onclicklistener on "playbutton",i able activity , in activity worked.can me on why button making app crash before activity , not on onclick?
04-05 23:15:15.886: e/androidruntime(11445): fatal exception: main 04-05 23:15:15.886: e/androidruntime(11445): java.lang.runtimeexception: unable start activity componentinfo{com.fullfrontalgames.numberfighter/com.fullfrontalgames.numberfighter.playafriend}: java.lang.nullpointerexception 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread.performlaunchactivity(activitythread.java:2136) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2174) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread.access$700(activitythread.java:141) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread$h.handlemessage(activitythread.java:1267) 04-05 23:15:15.886: e/androidruntime(11445): @ android.os.handler.dispatchmessage(handler.java:99) 04-05 23:15:15.886: e/androidruntime(11445): @ android.os.looper.loop(looper.java:137) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread.main(activitythread.java:5059) 04-05 23:15:15.886: e/androidruntime(11445): @ java.lang.reflect.method.invokenative(native method) 04-05 23:15:15.886: e/androidruntime(11445): @ java.lang.reflect.method.invoke(method.java:511) 04-05 23:15:15.886: e/androidruntime(11445): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) 04-05 23:15:15.886: e/androidruntime(11445): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:555) 04-05 23:15:15.886: e/androidruntime(11445): @ dalvik.system.nativestart.main(native method) 04-05 23:15:15.886: e/androidruntime(11445): caused by: java.lang.nullpointerexception 04-05 23:15:15.886: e/androidruntime(11445): @ com.fullfrontalgames.numberfighter.playafriend.oncreate(playafriend.java:54) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activity.performcreate(activity.java:5058) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1079) 04-05 23:15:15.886: e/androidruntime(11445): @ android.app.activitythread.performlaunchactivity(activitythread.java:2100) activity
package com.fullfrontalgames.numberfighter; import com.fullfrontalgames.numberfighter.dbadapter; import com.fullfrontalgames.numberfighter.r; import android.app.activity; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.support.v4.widget.simplecursoradapter; import android.view.view; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; public class playafriend extends activity{ dbadapter dbadapter; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.playafriend); final dbadapter db = new dbadapter(this); dbadapter = db.open(); listview friendlv = (listview) findviewbyid(android.r.id.list); button playbutton = (button) findviewbyid(r.id.playbutton); final textview friend = (textview) findviewbyid(r.id.textview_friends); cursor friendslist = db.getallfriends(); string[] = new string[] {"friends"}; // column/columns here int[] = new int[] {r.id.textview_friends}; @suppresswarnings("deprecation") listadapter cursoradapter = new simplecursoradapter(this, r.layout.list_items, friendslist, from, to,0); friendlv.setadapter(cursoradapter); playbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string defender = friend.gettext().tostring(); db.getfriend(defender); startactivity(new intent ("com.fullfrontalgames.numberfighter.fightattacker")); db.close(); } }); } } playafriend xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/nfbackground" android:orientation="vertical" > <imageview android:id="@id/titlebar" android:layout_width="wrap_content" android:layout_height="66dp" android:src="@drawable/nftitlebar" /> <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </listview> </linearlayout> list_items xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <framelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:background="@drawable/searchimageview" android:orientation="vertical" > <button android:id="@+id/playbutton" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="right" android:background="@drawable/playbutton" /> <textview android:id="@+id/textview_friends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/black" android:textstyle="bold" android:textsize="40dp" android:layout_gravity="center" /> </framelayout> </linearlayout>
please try code.
public class test extends activity{ dbadapter dbadapter; listview friendlv; button playbutton; final textview friend; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.playafriend); final dbadapter db = new dbadapter(this); db.open(); cursor friendslist = db.getallfriends(); if(friendslist.getcount()!=0){ friendlv = (listview) findviewbyid(android.r.id.list); playbutton = (button) findviewbyid(r.id.playbutton); friend = (textview) findviewbyid(r.id.textview_friends); string[] = new string[] {"friends"}; // column/columns here int[] = new int[] {r.id.textview_friends}; @suppresswarnings("deprecation") listadapter cursoradapter = new simplecursoradapter(this, r.layout.list_items, friendslist, from, to,0); friendlv.setadapter(cursoradapter); playbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string defender = friend.gettext().tostring(); db.getfriend(defender); startactivity(new intent ("com.fullfrontalgames.numberfighter.fightattacker")); db.close(); } }); } } } if getting problem let me know.
Comments
Post a Comment