java - Android about unable to start activity componentinfo -
i'm new programmer,recently have bug can't solve
java.lang.runtimeexception: unable start activity componentinfo{com.example.android.bl uetoothchat/com.example.android.bluetoothfiletran.filetranactivity}: java.lang.nullpointerexception
logcat stack trace
04-26 10:19:43.613: e/bluetoothchat(21754): +++ on create +++ 04-26 10:19:44.213: e/checkpermission(21754): _bluetooth code = 1 04-26 10:19:44.213: e/bluetoothchat(21754): ++ on start ++ 04-26 10:19:44.233: e/bluetoothchat(21754): + on resume + 04-26 10:19:44.233: e/checkpermission(21754): _bluetooth code = 13 04-26 10:19:45.643: e/bluetoothchat(21754): - on pause - 04-26 10:19:47.223: e/bluetoothchat(21754): + on resume + 04-26 10:19:55.093: e/bluetoothchat(21754): - on pause - 04-26 10:20:21.363: e/androidruntime(21754): fatal exception: main 04-26 10:20:21.363: e/androidruntime(21754): process: com.example.android.bluetoothchat, pid: 21754 04-26 10:20:21.363: e/androidruntime(21754): java.lang.runtimeexception: unable start activity componentinfo{com.example.android.bluetoothchat/com.example.android.bluetoothfiletran.filetranactivity}: java.lang.nullpointerexception 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread.performlaunchactivity(activitythread.java:2190) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2250) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread.access$800(activitythread.java:139) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread$h.handlemessage(activitythread.java:1200) 04-26 10:20:21.363: e/androidruntime(21754): @ android.os.handler.dispatchmessage(handler.java:102) 04-26 10:20:21.363: e/androidruntime(21754): @ android.os.looper.loop(looper.java:136) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread.main(activitythread.java:5049) 04-26 10:20:21.363: e/androidruntime(21754): @ java.lang.reflect.method.invokenative(native method) 04-26 10:20:21.363: e/androidruntime(21754): @ java.lang.reflect.method.invoke(method.java:515) 04-26 10:20:21.363: e/androidruntime(21754): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:818) 04-26 10:20:21.363: e/androidruntime(21754): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:634) 04-26 10:20:21.363: e/androidruntime(21754): @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132) 04-26 10:20:21.363: e/androidruntime(21754): @ dalvik.system.nativestart.main(native method) 04-26 10:20:21.363: e/androidruntime(21754): caused by: java.lang.nullpointerexception 04-26 10:20:21.363: e/androidruntime(21754): @ com.example.android.bluetoothfiletran.filetranactivity.oncreate(filetranactivity.java:66) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activity.performcreate(activity.java:5293) 04-26 10:20:21.363: e/androidruntime(21754): @ de.robv.android.xposed.xposedbridge.invokeoriginalmethodnative(native method) 04-26 10:20:21.363: e/androidruntime(21754): @ de.robv.android.xposed.xposedbridge.handlehookedmethod(xposedbridge.java:631) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activity.performcreate(native method) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-26 10:20:21.363: e/androidruntime(21754): @ android.app.activitythread.performlaunchactivity(activitythread.java:2154) 04-26 10:20:21.363: e/androidruntime(21754): ... 12 more
here bluetoooth.java
package com.example.android.bluetoothchat; import com.example.android.bluetoothchat.r; import com.example.android.bluetoothchat.service.bluetoothchatservice; import com.example.android.bluetoothfiletran.filetranactivity; import android.app.activity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.content.contentvalues; import android.content.intent; import android.net.uri; import android.os.bundle; import android.os.handler; import android.os.handlerthread; import android.os.message; import android.util.log; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.view.inputmethod.editorinfo; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class bluetoothchat extends activity { public static final string send_file_name = "sendfilename"; public static final int request_file = 1000; private static string msendfilename ; private handler mothhandler; private static string maddress ; private static final string tag = "bluetoothchat"; private static final boolean d = true; public static final int message_state_change = 1; public static final int message_read = 2; public static final int message_write = 3; public static final int message_device_name = 4; public static final int message_toast = 5; public static final string device_name = "device_name"; public static final string toast = "toast"; private static final int request_connect_device = 1; private static final int request_enable_bt = 2; private listview mconversationview; private edittext moutedittext; private button msendbutton; private string mconnecteddevicename = null; private arrayadapter<string> mconversationarrayadapter; private stringbuffer moutstringbuffer; private bluetoothadapter mbluetoothadapter = null; private bluetoothchatservice mchatservice = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if(d) log.e(tag, "+++ on create +++"); setcontentview(r.layout.main); mbluetoothadapter = bluetoothadapter.getdefaultadapter(); if (mbluetoothadapter == null) { toast.maketext(this, "bluetooth not available", toast.length_long).show(); finish(); return; } } @override public void onstart() { super.onstart(); if(d) log.e(tag, "++ on start ++"); if (!mbluetoothadapter.isenabled()) { intent enableintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enableintent, request_enable_bt); } else { if (mchatservice == null) setupchat(); } } private void setupchat() { log.d(tag, "setupchat()"); mconversationarrayadapter = new arrayadapter<string>(this, r.layout.message); mconversationview = (listview) findviewbyid(r.id.in); mconversationview.setadapter(mconversationarrayadapter); moutedittext = (edittext) findviewbyid(r.id.edit_text_out); moutedittext.setoneditoractionlistener(mwritelistener); msendbutton = (button) findviewbyid(r.id.button_send); msendbutton.setonclicklistener(new onclicklistener() { public void onclick(view v) { textview view = (textview) findviewbyid(r.id.edit_text_out); string message = view.gettext().tostring(); sendmessage(message); } }); mchatservice = new bluetoothchatservice(this, mhandler); moutstringbuffer = new stringbuffer(""); } @override public synchronized void onresume() { super.onresume(); if(d) log.e(tag, "+ on resume +"); if (mchatservice != null) { if (mchatservice.getstate() == bluetoothchatservice.state_none) { mchatservice.start(); } } } private textview.oneditoractionlistener mwritelistener = new textview.oneditoractionlistener() { public boolean oneditoraction(textview view, int actionid, keyevent event) { //editorinfo.ime_null keyevent.action_up if (actionid == editorinfo.ime_null && event.getaction() == keyevent.action_up) { string message = view.gettext().tostring(); sendmessage(message); } if(d) log.i(tag, "end oneditoraction"); return true; } }; @override public synchronized void onpause() { super.onpause(); if(d) log.e(tag, "- on pause -"); } @override public void onstop() { super.onstop(); if(d) log.e(tag, "-- on stop --"); } @override public void ondestroy() { super.ondestroy(); if (mchatservice != null) mchatservice.stop(); if(d) log.e(tag, "--- on destroy ---"); } private void sendmessage(string message) { if (mchatservice.getstate() != bluetoothchatservice.state_connected) { toast.maketext(this, r.string.not_connected, toast.length_short).show(); return; } if (message.length() > 0) { byte[] send = message.getbytes(); mchatservice.write(send); moutstringbuffer.setlength(0); moutedittext.settext(moutstringbuffer); } } private final handler mhandler = new handler() { @override public void handlemessage(message msg) { switch (msg.what) { case message_state_change: if(d) log.i(tag, "message_state_change: " + msg.arg1); switch (msg.arg1) { case bluetoothchatservice.state_connected: mconversationarrayadapter.clear(); break; case bluetoothchatservice.state_connecting: break; case bluetoothchatservice.state_listen: case bluetoothchatservice.state_none: break; } break; case message_write: byte[] writebuf = (byte[]) msg.obj; string writemessage = new string(writebuf); mconversationarrayadapter.add("me: " + writemessage); break; case message_read: byte[] readbuf = (byte[]) msg.obj; string readmessage = new string(readbuf, 0, msg.arg1); mconversationarrayadapter.add(mconnecteddevicename+": " + readmessage); break; case message_device_name: mconnecteddevicename = msg.getdata().getstring(device_name); // maddress = msg.getdata().getstring(device_address); toast.maketext(getapplicationcontext(), "connected " + mconnecteddevicename, toast.length_short).show(); break; case message_toast: toast.maketext(getapplicationcontext(), msg.getdata().getstring(toast), toast.length_short).show(); break; } } }; public void onactivityresult(int requestcode, int resultcode, intent data) { if(d) log.d(tag, "onactivityresult " + resultcode); switch (requestcode) { case request_connect_device: if (resultcode == activity.result_ok) { maddress = data.getextras() .getstring(devicelistactivity.extra_device_address); bluetoothdevice device = mbluetoothadapter.getremotedevice(maddress); mchatservice.connect(device); } break; case request_enable_bt: if (resultcode == activity.result_ok) { setupchat(); } else { log.d(tag, "bt not enabled"); toast.maketext(this, r.string.bt_not_enabled_leaving, toast.length_short).show(); finish(); } break; case request_file: if (resultcode == activity.result_ok) { msendfilename = data.getextras().getstring(send_file_name); dosendfilebybluetooth(); } break; } } private void dosendfilebybluetooth() { string filepath = msendfilename.trim(); if(!filepath.equals("null")){ if(null == mothhandler){ handlerthread handlerthread = new handlerthread("other_thread"); handlerthread.start(); mothhandler = new handler(handlerthread.getlooper()); } mothhandler.post(new runnable() { @override public void run() { contentvalues cv = new contentvalues(); string uri = "file://" + msendfilename.trim(); cv.put("uri", uri); cv.put("destination", maddress.trim()); cv.put("direction", 0); long ts = system.currenttimemillis(); cv.put("timestamp", ts); getcontentresolver().insert(uri.parse("content://com.android.bluetooth.opp/btopp"), cv); } }); }else { toast.maketext(bluetoothchat.this, "请选择要发送的文件!", toast.length_long).show(); } } private void ensurediscoverable() { if(d) log.d(tag, "ensure discoverable"); if (mbluetoothadapter.getscanmode() != bluetoothadapter.scan_mode_connectable_discoverable) { intent discoverableintent = new intent(bluetoothadapter.action_request_discoverable); discoverableintent.putextra(bluetoothadapter.extra_discoverable_duration, 300); startactivity(discoverableintent); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.option_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.scan: intent serverintent = new intent(this, devicelistactivity.class); startactivityforresult(serverintent, request_connect_device); return true; case r.id.discoverable: ensurediscoverable(); return true; case r.id.file_transport: intent fileintent = new intent(this, filetranactivity.class); startactivityforresult(fileintent, request_file); return true; } return false; } }
here filetranactivity.java
package com.example.android.bluetoothfiletran; import java.io.file; import com.example.android.bluetoothchat.bluetoothchat; import com.example.android.bluetoothchat.r; import com.example.android.bluetoothfiletran.adapter.adaptermanager; import com.example.android.bluetoothfiletran.adapter.filelistadapter; import android.app.activity; import android.content.intent; import android.graphics.color; import android.os.bundle; import android.os.environment; import android.os.handler; import android.os.handlerthread; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.button; import android.widget.listview; import android.widget.textview; import android.widget.toast; import android.widget.adapterview.onitemclicklistener; public class filetranactivity extends activity { listview mfilelistview; filelistadapter mfilelistadapter; adaptermanager madaptermanager; private handler motherhandler; private runnable updatefilelistrunnable; private file file; private string sdcardpath; private string path; button mbackbtn; button mensurebtn; button mcancelbtn; textview mlastclickview; textview mnowclickview; private boolean isselected = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.file_list); setresult(activity.result_canceled); mfilelistview = (listview) findviewbyid(r.id.filelistview); mbackbtn = (button) findviewbyid(r.id.selectfilebackbtn); mensurebtn = (button) findviewbyid(r.id.selectfileensurebtn); mcancelbtn = (button) findviewbyid(r.id.selectfilecancelbtn); sdcardpath = environment.getexternalstoragedirectory().getabsolutepath(); path = sdcardpath; madaptermanager = bluetoothapplication.getinstance().getadaptermanager(); mfilelistview.setadapter(madaptermanager.getfilelistadapter()); madaptermanager.updatefilelistadapter(path); mfilelistview.setonitemclicklistener(mfilelistonitemclicklistener); mbackbtn.setonclicklistener(mbackbtnclicklistener); mensurebtn.setonclicklistener(mensurebtnclicklistener); mcancelbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { filetranactivity.this.finish(); } }); } private onitemclicklistener mfilelistonitemclicklistener = new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { file = (file) mfilelistview.getadapter().getitem(position); if(file.isfile()){ if(null != mlastclickview){ mlastclickview.settextcolor(color.white); } mnowclickview = (textview) view.findviewbyid(r.id.filenametv); mnowclickview.settextcolor(color.blue); isselected = true; mlastclickview = mnowclickview; }else { path = file.getabsolutepath(); updatefilelist(); } } }; private onclicklistener mbackbtnclicklistener = new onclicklistener() { @override public void onclick(view v) { if(path.equals(sdcardpath)){ return ; } path = path.substring(0, path.lastindexof("/")); updatefilelist(); } }; private onclicklistener mensurebtnclicklistener = new onclicklistener() { @override public void onclick(view v) { if(!isselected){ toast.maketext(filetranactivity.this, "请选择文件!", toast.length_long).show(); return ; } intent intent = new intent(); intent.putextra(bluetoothchat.send_file_name, file.getabsolutepath()); setresult(activity.result_ok, intent); filetranactivity.this.finish(); } }; private void updatefilelist() { if(null != mlastclickview){ mlastclickview.settextcolor(color.white); mlastclickview = null; isselected = false; } if(null == updatefilelistrunnable){ updatefilelistrunnable = new runnable() { @override public void run() { madaptermanager.updatefilelistadapter(path); } }; } if(null == motherhandler){ handlerthread handlerthread = new handlerthread("other_thread"); handlerthread.start(); motherhandler = new handler(handlerthread.getlooper()); } motherhandler.post(updatefilelistrunnable); } }
then ,i find point out error in filetranactivity.java
madaptermanager = bluetoothapplication.getinstance().getadaptermanager();
so ,here bluetoothapplication.java
package com.example.android.bluetoothfiletran; import com.example.android.bluetoothfiletran.adapter.adaptermanager; import com.example.android.bluetoothfiletran.adapter.util.touchobject; import android.app.application; public class bluetoothapplication extends application { private static bluetoothapplication application; private adaptermanager madaptermanager; private touchobject mtouchobject; @override public void oncreate() { super.oncreate(); if(null == application){ application = this; } mtouchobject = new touchobject(); } public static bluetoothapplication getinstance(){ return application; } public adaptermanager getadaptermanager() { return madaptermanager; } public void setadaptermanager(adaptermanager adaptermanager) { this.madaptermanager = adaptermanager; } public touchobject gettouchobject() { return mtouchobject; } public void settouchobject(touchobject touchobject) { this.mtouchobject = touchobject; } }
and adaptermanager.java
package com.example.android.bluetoothfiletran.adapter; import java.io.file; import java.util.arraylist; import java.util.list; import android.bluetooth.bluetoothdevice; import android.content.context; import android.os.handler; import com.example.android.bluetoothchat.r; import com.example.android.bluetoothfiletran.adapter.filelistadapter; public class adaptermanager { private context mcontext; private filelistadapter mfilelistadapter; private list<bluetoothdevice> mdevicelist; private list<file> mfilelist; private handler mainhandler; public adaptermanager(context context){ this.mcontext = context; } public filelistadapter getfilelistadapter(){ if(null == mfilelistadapter){ mfilelist = new arraylist<file>(); mfilelistadapter = new filelistadapter(mcontext, mfilelist, r.layout.file_list_item); } return mfilelistadapter; } public void cleardevice(){ if(null != mdevicelist){ mdevicelist.clear(); } } public void adddevice(bluetoothdevice bluetoothdevice){ mdevicelist.add(bluetoothdevice); } public void changedevice(int listid, bluetoothdevice bluetoothdevice){ mdevicelist.remove(listid); mdevicelist.add(listid, bluetoothdevice); } public void updatefilelistadapter(string path){ mfilelist.clear(); mfilelist.addall(getfilelist(path)); if(null == mainhandler){ mainhandler = new handler(mcontext.getmainlooper()); } mainhandler.post(new runnable() { @override public void run() { mfilelistadapter.notifydatasetchanged(); } }); } public list<bluetoothdevice> getdevicelist() { return mdevicelist; } public static list<file> getfilelist(string path){ list<file> filelist = new arraylist<file>(); file[] files = new file(path).listfiles(); if(files.length > 0){ list<file> allfolder = new arraylist<file>(); list<file> allfile = new arraylist<file>(); for(file file : files){ if(file.isfile()){ allfile.add(file); }else { allfolder.add(file); } } filelist.addall(allfolder); filelist.addall(allfile); } return filelist; } }
my xml is
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.bluetoothchat" android:versioncode="1" android:versionname="1.0" xmlns:tools="http://schemas.android.com/tools"> <uses-sdk android:minsdkversion="6" android:targetsdkversion="18" tools:ignore="oldtargetapi"/> <uses-permission android:name="android.permission.bluetooth_admin" /> <uses-permission android:name="android.permission.bluetooth" /> <uses-permission android:name="android.permission.mount_unmount_filesystems"/> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/apptheme" > <activity android:name="com.example.android.bluetoothchat.bluetoothchat" 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="com.example.android.bluetoothchat.devicelistactivity" android:label="@string/select_device" android:theme="@android:style/theme.dialog" /> <activity android:name="com.example.android.bluetoothfiletran.filetranactivity" android:label="@string/select_file" android:theme="@android:style/theme.dialog" /> </application> </manifest>
register in androidmanifest
bluetoothfiletran
class application name in application
tag:
<application android:allowbackup="true" android:name="com.example.android.bluetoothfiletran" ...
without this, bluetoothfiletran
not loaded, , oncreate
never invoked.
this return null reference in bluetoothapplication.getinstance()
, causing nullpointerexception
.
notice: package in manifest, package="com.example.android.bluetoothchat"
different package bluetoothfiletran
located (not in 1 of childs). recommend use package com.example.android
instead of com.example.android.bluetoothchat
in manifest tag.
Comments
Post a Comment