Clicking back button causes force close / NullPointerException - java

I'm experiencing a NullPointerException and ForceClose issue when clicking the back button after launching either the gallery or the default audio player using the android application I've built.
I've seen a few examples that state I'll need to finish the activity or check for a back button press - but I'm not sure how exactly that would fit into this scenario.
It's crashing on line 84 which is: Uri selectedImage = data.getData();
Source:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
{
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
}
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
LogCat:
04-24 21:04:26.441: D/AndroidRuntime(4491): Shutting down VM
04-24 21:04:26.441: W/dalvikvm(4491): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:26.451: E/AndroidRuntime(4491): FATAL EXCEPTION: main
04-24 21:04:26.451: E/AndroidRuntime(4491): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:26.451: E/AndroidRuntime(4491): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:26.451: E/AndroidRuntime(4491): Caused by: java.lang.NullPointerException
04-24 21:04:26.451: E/AndroidRuntime(4491): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:26.451: E/AndroidRuntime(4491): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:26.451: E/AndroidRuntime(4491): ... 11 more
04-24 21:04:28.721: I/Process(4491): Sending signal. PID: 4491 SIG: 9
04-24 21:04:39.681: D/AndroidRuntime(4543): Shutting down VM
04-24 21:04:39.681: W/dalvikvm(4543): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:04:39.691: E/AndroidRuntime(4543): FATAL EXCEPTION: main
04-24 21:04:39.691: E/AndroidRuntime(4543): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.os.Looper.loop(Looper.java:137)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:04:39.691: E/AndroidRuntime(4543): at dalvik.system.NativeStart.main(Native Method)
04-24 21:04:39.691: E/AndroidRuntime(4543): Caused by: java.lang.NullPointerException
04-24 21:04:39.691: E/AndroidRuntime(4543): at com.tablet.hairmax.UI.onActivityResult(UI.java:76)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:04:39.691: E/AndroidRuntime(4543): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:04:39.691: E/AndroidRuntime(4543): ... 11 more
04-24 21:05:14.191: D/AndroidRuntime(4586): Shutting down VM
04-24 21:05:14.191: W/dalvikvm(4586): threadid=1: thread exiting with uncaught exception (group=0x41df5930)
04-24 21:05:14.191: E/AndroidRuntime(4586): FATAL EXCEPTION: main
04-24 21:05:14.191: E/AndroidRuntime(4586): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.tablet.hairmax/com.tablet.hairmax.UI}: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.access$1100(ActivityThread.java:141)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.os.Looper.loop(Looper.java:137)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-24 21:05:14.191: E/AndroidRuntime(4586): at dalvik.system.NativeStart.main(Native Method)
04-24 21:05:14.191: E/AndroidRuntime(4586): Caused by: java.lang.NullPointerException
04-24 21:05:14.191: E/AndroidRuntime(4586): at com.tablet.hairmax.UI.onActivityResult(UI.java:84)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
04-24 21:05:14.191: E/AndroidRuntime(4586): at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
04-24 21:05:14.191: E/AndroidRuntime(4586): ... 11 more

data is null. You need to check before that line if data is null. Since you are pressing the Back button, you aren't sending an Intent back with setResult() like would normally be done.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if (resultCode == Activity.RESULT_OK) {
if (data != null)
{
if (requestCode == SELECT_VIDEO) {
Uri selectedVideo = data.getData();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedVideo, "video/*");
startActivity(intent);
} else if (requestCode == SELECT_PHOTO) {
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(selectedImage, "image/*");
startActivity(intent);
}
}
See this answer about getting an image from gallery

Related

Android Radio Player m3u8 error

04-24 21:51:06.092: I/global(406): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-24 21:51:06.296: D/PlsParser(406): Playlist Url = http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.296: I/System.out(406): Playlist: Selected url - http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.616: E/MediaPlayer(406): error (1, -2147483648)
04-24 21:51:06.622: E/MediaPlayer(406): Error (1,-2147483648)
04-24 21:51:06.622: I/System.out(406): RadioService: stop
04-24 21:51:06.632: E/MediaPlayer(406): stop called in state 0
04-24 21:51:06.632: E/MediaPlayer(406): error (-38, 0)
04-24 21:51:06.652: V/ERROR(406): MEDIA ERROR UNKNOWN -2147483648
04-24 21:51:06.672: E/MediaPlayer(406): stop called in state 1
04-24 21:51:06.672: E/MediaPlayer(406): error (-38, 0)
04-24 21:51:06.692: D/PlsParser(406): Setting next stream from playlist: 0
04-24 21:51:06.692: D/PlsParser(406): URL = http:/ / 85 dot 204 dot 233 dot 122/radio-01/stream-high-02/playlist.m3u8?wowzasessionid=1094958256
04-24 21:51:06.822: I/global(406): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-24 21:51:07.002: D/PlsParser(406): Playlist Url = http:/ / 85 dot 204 dot 233 dot 122:1935/radio-01/_definst_/stream-high-02/key.m3u8key?wowzasessionid=1094958256"
04-24 21:51:07.002: W/dalvikvm(406): threadid=18: thread exiting with uncaught exception (group=0x4001d800)
04-24 21:51:07.002: E/AndroidRuntime(406): FATAL EXCEPTION: Thread-21
04-24 21:51:07.002: E/AndroidRuntime(406): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
04-24 21:51:07.002: E/AndroidRuntime(406): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-24 21:51:07.002: E/AndroidRuntime(406): at java.util.ArrayList.get(ArrayList.java:311)
04-24 21:51:07.002: E/AndroidRuntime(406): at com.my.radio.RadioService$3.run(RadioService.java:294)
04-24 21:51:07.002: E/AndroidRuntime(406): at java.lang.Thread.run(Thread.java:1096)
04-24 21:51:07.432: D/webviewglue(406): nativeDestroy view: 0x3037c0
04-24 21:51:07.442: D/webviewglue(406): nativeDestroy view: 0x303450
04-24 21:51:08.022: D/dalvikvm(406): GC_EXPLICIT freed 5811 objects / 296264 bytes in 501ms
04-24 21:51:08.412: E/ActivityThread(406): Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.BaseActivity$1#45fd26a8 that was originally bound here
04-24 21:51:08.412: E/ActivityThread(406): android.app.ServiceConnectionLeaked: Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.BaseActivity$1#45fd26a8 that was originally bound here
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ContextImpl.bindService(ContextImpl.java:863)
04-24 21:51:08.412: E/ActivityThread(406): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
04-24 21:51:08.412: E/ActivityThread(406): at com.my.radio.BaseActivity.onCreate(BaseActivity.java:39)
04-24 21:51:08.412: E/ActivityThread(406): at com.my.radio.MainActivity.onCreate(MainActivity.java:79)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 21:51:08.412: E/ActivityThread(406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:51:08.412: E/ActivityThread(406): at android.os.Looper.loop(Looper.java:123)
04-24 21:51:08.412: E/ActivityThread(406): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 21:51:08.412: E/ActivityThread(406): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:51:08.412: E/ActivityThread(406): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 21:51:08.412: E/ActivityThread(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 21:51:08.412: E/ActivityThread(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 21:51:08.412: E/ActivityThread(406): at dalvik.system.NativeStart.main(Native Method)
04-24 21:51:08.463: E/ActivityThread(406): Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.MainActivity$2#45fd30d8 that was originally bound here
04-24 21:51:08.463: E/ActivityThread(406): android.app.ServiceConnectionLeaked: Activity com.my.radio.MainActivity has leaked ServiceConnection com.my.radio.MainActivity$2#45fd30d8 that was originally bound here
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ContextImpl.bindService(ContextImpl.java:863)
04-24 21:51:08.463: E/ActivityThread(406): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
04-24 21:51:08.463: E/ActivityThread(406): at com.my.radio.MainActivity.onCreate(MainActivity.java:85)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 21:51:08.463: E/ActivityThread(406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 21:51:08.463: E/ActivityThread(406): at android.os.Looper.loop(Looper.java:123)
04-24 21:51:08.463: E/ActivityThread(406): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 21:51:08.463: E/ActivityThread(406): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:51:08.463: E/ActivityThread(406): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 21:51:08.463: E/ActivityThread(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 21:51:08.463: E/ActivityThread(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 21:51:08.463: E/ActivityThread(406): at dalvik.system.NativeStart.main(Native Method)
04-24 21:51:09.452: I/Process(406): Sending signal. PID: 406 SIG: 9
welcome to Stackoverflow.com :), i think you have an IndexOutofBounds Exception: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
this is an example of how i play an .M3U8 using a VideoView:
private VideoView myvideoview;
private String urlStream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String streamV = "http://media.jorgesys.com/reforma.m3u8";
new playM3U8().execute(streamV);
myvideoview = (VideoView)this.findViewById(R.id.myvideoview);
myvideoview.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
myvideoview.start();
}
});
myvideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.i("Exception", "url: " + url);
return false;
}
});
}
This class is an AyncTask to play the M3U8:
class playM3U8 extends AsyncTask<String, Void, Void> {
private Exception exception;
protected Void doInBackground(String... urls) {
urlStream = urls[0];
runOnUiThread(new Runnable() {
#Override
public void run() {
myvideoview.setVideoURI(Uri.parse(urlStream));
}
});
return null;
}
protected void onPostExecute(String feed) {
Log.i("*** Streaming M3U8", "url: " + urlStream );
}
}

Spinner problems with Sharedpreferences

Sorry,this is my first time to put questions in this site
My Function: I set two Buttons to restore and save the information in the Spinner and EditText with the function Sharedpreferences.
I execute the program at the first time. The program will appear the error state,if I click the Button "restore" to restore the information in Spinner. But I haven't met the problem when I restore the information in EditText.
This is the code in Spinner
private Spinner.OnItemSelectedListener getfeet = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
feet_out = parent.getSelectedItemPosition() + 2;
select_f = feet.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
private Spinner.OnItemSelectedListener getinch = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
inch_out = parent.getSelectedItemPosition();
select_i = inch.getSelectedItemPosition(); //save the position you choose
Toast.makeText(MainActivity.this,
"you chose " + parent.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
This is the code which executes the function of save in Sharedpreferences
private void save_() {
settings = getSharedPreferences("DATA", 0);
settings.edit().putInt("DATA_FEET", select_f) //store the position in DATA_FEET and DATA_INCH
.putInt("DATA_INCH", select_i)
.putString("DATA_WEIGHT", weight.getText().toString()).commit();
Toast.makeText(MainActivity.this, R.string.done, Toast.LENGTH_SHORT) //save done
.show();
}
This is the code which executes the function of restore in Sharedpreferences
private void restore_() {
feet.setSelection(settings.getInt("DATA_FEET", select_f)); //restore the position
inch.setSelection(settings.getInt("DATA_INCH", select_i));
weight.setText(settings.getString("DATA_WEIGHT", "EMPTY"));
}
My problem is that I can't use the function of restore at the program executing at the first time. Is there any solution to solve the problem?? Because it is normal in EditText,but it is abnormal in Spinner. Thanks :))
This is the state. :))
10-23 23:14:11.677: D/TextLayoutCache(26370): Using debug level: 0 - Debug Enabled: 0
10-23 23:14:11.747: D/libEGL(26370): loaded /system/lib/egl/libGLES_android.so
10-23 23:14:11.797: D/libEGL(26370): loaded /system/lib/egl/libEGL_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-23 23:14:11.827: D/libEGL(26370): loaded /system/lib/egl/libGLESv2_mali.so
10-23 23:14:11.887: D/OpenGLRenderer(26370): Enabling debug mode 0
10-23 23:14:16.762: D/AndroidRuntime(26370): Shutting down VM
10-23 23:14:16.762: W/dalvikvm(26370): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-23 23:14:16.802: E/AndroidRuntime(26370): FATAL EXCEPTION: main
10-23 23:14:16.802: E/AndroidRuntime(26370): java.lang.NullPointerException
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View.performClick(View.java:3574)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.view.View$PerformClick.run(View.java:14293)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.handleCallback(Handler.java:605)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.os.Looper.loop(Looper.java:137)
10-23 23:14:16.802: E/AndroidRuntime(26370): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 23:14:16.802: E/AndroidRuntime(26370): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-23 23:14:16.802: E/AndroidRuntime(26370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-23 23:14:16.802: E/AndroidRuntime(26370): at dalvik.system.NativeStart.main(Native Method)
This is the method to call the restore_()
private OnClickListener reback_1 = new OnClickListener() {
public void onClick(View v) {
restore_();
}
};
After I replaced the select_f and select_i into 0, it appeared the problem
10-24 00:01:30.957: D/TextLayoutCache(28836): Using debug level: 0 - Debug Enabled: 0
10-24 00:01:31.017: D/libEGL(28836): loaded /system/lib/egl/libGLES_android.so
10-24 00:01:31.037: D/libEGL(28836): loaded /system/lib/egl/libEGL_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-24 00:01:31.057: D/libEGL(28836): loaded /system/lib/egl/libGLESv2_mali.so
10-24 00:01:31.087: D/OpenGLRenderer(28836): Enabling debug mode 0
10-24 00:01:36.262: D/AndroidRuntime(28836): Shutting down VM
10-24 00:01:36.262: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x40aaa210)
10-24 00:01:36.282: E/AndroidRuntime(28836): FATAL EXCEPTION: main
10-24 00:01:36.282: E/AndroidRuntime(28836): java.lang.NullPointerException
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.restore_(MainActivity.java:44)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity.access$1(MainActivity.java:43)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.example.bmi.MainActivity$2.onClick(MainActivity.java:99)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View.performClick(View.java:3574)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.view.View$PerformClick.run(View.java:14293)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.handleCallback(Handler.java:605)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.os.Looper.loop(Looper.java:137)
10-24 00:01:36.282: E/AndroidRuntime(28836): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 00:01:36.282: E/AndroidRuntime(28836): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-24 00:01:36.282: E/AndroidRuntime(28836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-24 00:01:36.282: E/AndroidRuntime(28836): at dalvik.system.NativeStart.main(Native Method)
10-24 00:01:37.803: I/Process(28836): Sending signal. PID: 28836 SIG: 9
You are using the SharedPrefs wrong.
feet.setSelection(settings.getInt("DATA_FEET", select_f));
when you try to fetch the integer-value from sharedPref, you get returned null! because i think that you misunderstand how it is done:
settings.getInt("myKey", defaultValue);
returns you the value for the key "myKey". if "myKey" has no value set, then it returns you the "defaultValue". In your case it returns the current value of "select_f". And as it looks, the value of select_f is null, when you run your application for the first time.
So you have to decide whether to initialize "select_f" before you retrieve the sharedPrefs or you enter another defaultValue here.

Java app crashes (NullPointerException)

i'm just programming a password manager but every time I run it, it crashed in cause of NullPointerException. I checked the code but i'm a new developer. Someone here who knows why? :(
Code of MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
static String masterpass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpass = "";
masterpassfield = (EditText) findViewById(R.id.masterpass);
if(masterpass.equals("")) {
Intent intent = new Intent(this,NewMasterpass.class);
startActivity(intent);
return;
}
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(masterpassfield.getText().toString().equals(masterpass)) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Code of NewMasterpass.java:
package de.lennartschoch.passwordmanager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NewMasterpass extends Activity {
EditText masterpasscreate;
EditText repeat;
Button finish;
TextView mistake;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_masterpass);
masterpasscreate = (EditText) findViewById(R.id.masterpasscreate);
repeat = (EditText) findViewById(R.id.repeatcreate);
finish = (Button) findViewById(R.id.finish);
mistake = (TextView) findViewById(R.id.mistake);
finish.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(!masterpasscreate.getText().toString().equals(repeat.getText().toString())) {
mistake.setText("The passwords are different.");
mistake.setTextColor(Color.RED);
}
else if(masterpasscreate.getText().toString().equals("") || repeat.getText().toString().equals("")) {
mistake.setText("Both textfields must be filled in");
mistake.setTextColor(Color.RED);
}
else {
MainActivity.masterpass = masterpasscreate.getText().toString();
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
}
}});
}
}
LogCat error:
01-20 17:17:52.629: D/AndroidRuntime(561): Shutting down VM
01-20 17:17:52.629: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:17:52.639: E/AndroidRuntime(561): FATAL EXCEPTION: main
01-20 17:17:52.639: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivit y}: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.os.Looper.loop(Looper.java:137)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:17:52.639: E/AndroidRuntime(561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:17:52.639: E/AndroidRuntime(561): at dalvik.system.NativeStart.main(Native Method)
01-20 17:17:52.639: E/AndroidRuntime(561): Caused by: java.lang.NullPointerException
01-20 17:17:52.639: E/AndroidRuntime(561): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:26)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:17:52.639: E/AndroidRuntime(561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:17:52.639: E/AndroidRuntime(561): ... 11 more
01-20 17:17:57.129: I/Process(561): Sending signal. PID: 561 SIG: 9
01-20 17:21:37.499: D/AndroidRuntime(608): Shutting down VM
01-20 17:21:37.499: W/dalvikvm(608): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:21:37.511: E/AndroidRuntime(608): FATAL EXCEPTION: main
01-20 17:21:37.511: E/AndroidRuntime(608): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:137)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:21:37.511: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:21:37.511: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
01-20 17:21:37.511: E/AndroidRuntime(608): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:21:37.511: E/AndroidRuntime(608): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:21:37.511: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:21:37.511: E/AndroidRuntime(608): ... 11 more
01-20 17:22:30.769: D/AndroidRuntime(655): Shutting down VM
01-20 17:22:30.769: W/dalvikvm(655): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-20 17:22:30.779: E/AndroidRuntime(655): FATAL EXCEPTION: main
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.os.Looper.loop(Looper.java:137)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 17:22:30.779: E/AndroidRuntime(655): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 17:22:30.779: E/AndroidRuntime(655): at dalvik.system.NativeStart.main(Native Method)
01-20 17:22:30.779: E/AndroidRuntime(655): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass}; have you declared this activity in your AndroidManifest.xml?
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.startActivity(Activity.java:3297)
01-20 17:22:30.779: E/AndroidRuntime(655): at de.lennartschoch.passwordmanager.MainActivity.onCreate(MainActivity.java:30)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Activity.performCreate(Activity.java:4465)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 17:22:30.779: E/AndroidRuntime(655): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-20 17:22:30.779: E/AndroidRuntime(655): ... 11 more
01-20 17:22:33.180: I/Process(655): Sending signal. PID: 655 SIG: 9
It asks:
01-20 17:22:30.779: E/AndroidRuntime(655): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {de.lennartschoch.passwordmanager/de.lennartschoch.passwordmanager.NewMasterpass};
have you declared this activity in your AndroidManifest.xml?
Have you?
Declare the NewMasterPass activity in your manifest xml as clearly mentioned in the logcat!
Try to add this to your AndroidManifest.xml
<activity android:name="de.lennartschoch.passwordmanager.NewMasterpass" />

reading database into app/ displaying it in listview

I am trying to populate a listview using a database that i have saved in my assets. I have gotten as far as checking to see if the database is being created when i run the app(which it is). My current problem is trying to figure out how to get the columns and display them in a list view. My columns are name,position and department, in case you see a piece of code you don't understand. i've been trying to use code from http://saigeethamn.blogspot.com/2011/02/listview-of-data-from-sqlitedatabase.html
if anybody is able to help me, I would be really grateful, Thank You in Advanced.
EDIT: When code runs it gives me a null pointer exception
public class Favorites extends ListActivity {
public static String tableName = "fac";
private ListView lv;
SQLiteDatabase newDB;
private ArrayList<String> results = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("This data is retrieved from the database and only 4 "
+ "of the results are displayed");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
DataBaseHelper dbHelper = new DataBaseHelper(
this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName
+ "where ", null);
if (c != null) {
if (c.moveToFirst()) {
do {
String name = c.getString(c.getColumnIndex("name"));
String position = c.getString(c
.getColumnIndex("position"));
results.add("name: " + name + ",Position " + position);
} while (c.moveToNext());
}
}
} catch (SQLiteException se) {
} finally {
if (newDB != null)
{
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}}
}
}
LogCat:
04-24 04:43:19.064: E/AndroidRuntime(1644): FATAL EXCEPTION: main
04-24 04:43:19.064: E/AndroidRuntime(1644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Faculty_tab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.os.Looper.loop(Looper.java:143)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.main(ActivityThread.java:4196)
04-24 04:43:19.064: E/AndroidRuntime(1644): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 04:43:19.064: E/AndroidRuntime(1644): at java.lang.reflect.Method.invoke(Method.java:507)
04-24 04:43:19.064: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 04:43:19.064: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 04:43:19.064: E/AndroidRuntime(1644): at dalvik.system.NativeStart.main(Native Method)
04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.widget.TabHost.addTab(TabHost.java:216)
04-24 04:43:19.064: E/AndroidRuntime(1644): at com.cerealBarApps.Faculty_tab.onCreate(Faculty_tab.java:55)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
04-24 04:43:19.064: E/AndroidRuntime(1644): ... 11 more
04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.NullPointerException
04-24 04:43:19.064: E/AndroidRuntime(1644): at com.cerealBarApps.Favorites.openAndQueryDatabase(Favorites.java:63)
04-24 04:43:19.064: E/AndroidRuntime(1644): at com.cerealBarApps.Favorites.onCreate(Favorites.java:25)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-24 04:43:19.064: E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
Sorry
Try this plz..
In the below statment, please put ' ' before 'where' keyword.
Old
Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + "where ", null);
New
Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + " where ", null);

Android Custom Dialog implies a Force Close

I'm calling a dialog function on my code, but everytime I test it it makes a force close...
Here's my code:
public void onLongPressFinished(MotionEvent arg0,
ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem item) {
if (item!= null) // if the user longpresses a marker, it will pop up the route dialog
{
RouteDialog dialog = new RouteDialog();
dialog.showCustomDialog();
}
and the class I use to implement the dialog:
package com.BuStopTracker;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RouteDialog extends Activity{
public void showCustomDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.routetodialog);//loads layout from xml file
dialog.setTitle("Título do custom dialog");
final Button ok = (Button) dialog.findViewById(R.id.bt_ok);
final Button cancelar = (Button) dialog.findViewById(R.id.bt_cancel);
final EditText editText = (EditText) dialog.findViewById(R.id.inputText);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//OK button action
}
});
cancelar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
I know that my "LongPress detector" function is working, because I tested it with a toast, and it worked fine...
So, can someone help me? I'm pretty sure my fault is in the way I call the dialog, I just can't see why or where...
Thanks.
PS: As requested, here's the logcat:
11-13 20:13:54.789: D/dalvikvm(406): GC_CONCURRENT freed 1153K, 54% free 3159K/6727K, external 2002K/2137K, paused 4ms+4ms
11-13 20:13:55.109: D/dalvikvm(406): GC_CONCURRENT freed 616K, 51% free 3363K/6727K, external 2002K/2137K, paused 5ms+7ms
11-13 20:13:55.369: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 721K, 54% free 3116K/6727K, external 2121K/2137K, paused 52ms
11-13 20:13:55.829: I/MapActivity(406): Handling network change notification:CONNECTED
11-13 20:13:55.829: E/MapActivity(406): Couldn't get connection factory client
11-13 20:13:55.979: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 215K, 56% free 3020K/6727K, external 2440K/2692K, paused 77ms
11-13 20:14:17.679: D/AndroidRuntime(406): Shutting down VM
11-13 20:14:17.679: W/dalvikvm(406): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-13 20:14:17.719: E/AndroidRuntime(406): FATAL EXCEPTION: main
11-13 20:14:17.719: E/AndroidRuntime(406): java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Activity.getSystemService(Activity.java:3536)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Dialog.<init>(Dialog.java:141)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.Dialog.<init>(Dialog.java:123)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.BuStopTracker.RouteDialog.showCustomDialog(RouteDialog.java:23)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.BuStopTracker.BusTrackerBetaActivity$7.onLongPressFinished(BusTrackerBetaActivity.java:278)
11-13 20:14:17.719: E/AndroidRuntime(406): at de.android1.overlaymanager.ManagedOverlayGestureDetector.invokeLongPressFinished(ManagedOverlayGestureDetector.java:76)
11-13 20:14:17.719: E/AndroidRuntime(406): at de.android1.overlaymanager.ManagedOverlay.draw(ManagedOverlay.java:79)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.Overlay.draw(Overlay.java:179)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.google.android.maps.MapView.onDraw(MapView.java:530)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6880)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.draw(ViewRoot.java:1522)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.os.Looper.loop(Looper.java:130)
11-13 20:14:17.719: E/AndroidRuntime(406): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 20:14:17.719: E/AndroidRuntime(406): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:14:17.719: E/AndroidRuntime(406): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 20:14:17.719: E/AndroidRuntime(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 20:14:17.719: E/AndroidRuntime(406): at dalvik.system.NativeStart.main(Native Method)
11-13 20:14:20.059: I/Process(406): Sending signal. PID: 406 SIG: 9
You should not extend Activity in your RouteDialog. Pass a Context into showCustomDialog or make a contructor taking it and store it in your class.
To fix is easy change this, in RouteDialog:
public class RouteDialog {
public void showCustomDialog(Context context){
final Dialog dialog = new Dialog(context);
/* leave rest as is */
And then change your code to show the dialog to:
RouteDialog dialog = new RouteDialog();
dialog.showCustomDialog(this);

Categories