简体   繁体   English

是否为contextWrapper.getResources()提供了空指针异常? 使用自定义侦听器时

[英]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. 对于我之前实现的常见类型的自定义侦听器,得到一个奇怪的空指针异常,没有任何问题。

a crash will happen if i put any code in the callback method that communicates with the main user interface thread of the activity. 如果我在与该活动的主用户界面线程通信的回调方法中放入任何代码,就会发生崩溃。 if i put any code inside this method that does not touch the user interface thread then there is no crash. 如果我在此方法中放入不接触用户界面线程的任何代码,则不会崩溃。 it does not make sense because all the code in this class runs on the UI thread and does not need to use a handler or runonui method. 这没有意义,因为此类中的所有代码都在UI线程上运行,并且不需要使用处理程序或runonui方法。

how do i fix this problem? 我该如何解决这个问题?

from the logcat: 从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.<init>(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 StartActivity类中侦听器的代码,这是不扩展活动的实用程序类,此处的所有代码均在与UI线程分开的新线程运行方法中运行

in class variables declarations 在类变量声明中

 public OnResultReturnedListener listener;

in oncreate 在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 实现侦听器的另一个类中的代码,所有代码都在UI主线程上运行,没有创建其他线程

 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;
    }
 }

I believe the problem is in listener = new StartActivity(). 我相信问题出在监听器= new StartActivity()。 You have created activity manually that's why it doesn't have attached context and unable to interact with ui thread. 您手动创建了活动,这就是为什么它没有附加的上下文并且无法与ui线程进行交互的原因。 You can pass StartActivity reference to your Synchronizer. 您可以将StartActivity参考传递给Synchronizer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM