null pointer exception for contextWrapper.getResources()? when using
custom listener
getting a strange null pointer exception for a common type of custom
listener that i have implemented before without any problems.
how do i fix this problem?
from the logcat:
09-30 10:55:15.360: E/AndroidRuntime(2207): FATAL EXCEPTION: main 09-30
10:55:15.360: E/AndroidRuntime(2207): java.lang.NullPointerException 09-30
10:55:15.360: E/AndroidRuntime(2207): at
android.content.ContextWrapper.getResources(ContextWrapper.java:81) 09-30
10:55:15.360: E/AndroidRuntime(2207): at
android.widget.Toast.(Toast.java:92) 09-30 10:55:15.360:
E/AndroidRuntime(2207): at android.widget.Toast.makeText(Toast.java:233)
09-30 10:55:15.360: E/AndroidRuntime(2207): at
StartActivity.onResultReturned(StartActivity.java:124) 09-30 10:55:15.360:
E/AndroidRuntime(2207): at
jp.co.forever.tankinspectionsystem.Synchronizer.sendInt(Synchronizer.java:215)
09-30 10:55:15.360: E/AndroidRuntime(2207): at
jp.co.forever.tankinspectionsystem.Synchronizer$SendOutMsgAndPack$2.run(Synchronizer.java:162)
code for the listener in the StartActivity class, this is utility class
that does not extend activity, and all the code here runs in a new thread
run method separate from the UI thread
in class variables declarations
public OnResultReturnedListener listener;
in oncreate
listener = new StartActivity();
listener is an interface as a nested subclass in this activity class
public interface OnResultReturnedListener {
public void onResultReturned(int result);
}
code in the other class that implements the listener, all code runs on UI
main thread, no additional thread are created
public class StartActivity extends Activity
implements Synchronizer.OnResultReturnedListener {
// method onResultReturned is override for custom listerner class
// OnResultReturnedListener interface inside of the Synchronizer class
@Override
public void onResultReturned(int result) {
// toast like this or any other method that touched the
// UI thread will result in a crash
Toast.makeText(StartActivity.this, "value returned "
+ result , Toast.LENGTH_SHORT).show();
// putting the Toast in a handler will not help, still results in crash
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(StartActivity.this, "value returned "
+ result, Toast.LENGTH_SHORT).show();
}
});
//adding a handler.post to the other class, Synchronizer,
// that the method call comes from
// will not help and still causes crashes, for example
//handler.post(new Runnable() {
//@Override
//public void run() {
//fileTransferStatus = 1;
// listener.onResultReturned(fileTransferStatus);
// }
//});
// this statement will not cause crash
int x = 13;
}
}
No comments:
Post a Comment