首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MainActivity中从另一个类- NullPointerException调用方法

在MainActivity中从另一个类- NullPointerException调用方法
EN

Stack Overflow用户
提问于 2022-02-17 13:37:55
回答 3查看 62关注 0票数 0

我有两个类,MainActivity类和一个连接USB设备的类(CognexHandler)。

当设备连接完成后,我想在MainActivity类中调用一个方法,该方法包含TextViwes到activity_main.xml文件。

关于第一个TextView的初始化-

代码语言:javascript
复制
TextView tvScanToLogin = (TextView) findViewById(R.id.tvScanToLogin);

该程序使用以下错误代码崩溃:

代码语言:javascript
复制
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:183)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at android.content.Context.obtainStyledAttributes(Context.java:763)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:848)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
        at de.egeplast.logistikscanner_test.MainActivity.getBatteryLevelPhone(MainActivity.java:155)
        at de.egeplast.logistikscanner_test.CognexHandler.onConnectionCompleted(CognexHandler.java:192)
        at com.cognex.mobile.barcode.sdk.ReaderDevice$7.onResponseReceived(ReaderDevice.java:668)
        at com.cognex.dataman.sdk.CommandInfo$1.run(CommandInfo.java:134)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

在这里,课程代码:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity implements
        ReaderDevice.ReaderDeviceListener, 
        ReaderDevice.OnConnectionCompletedListener, 
        ActivityCompat.OnRequestPermissionsResultCallback{


        @Override
        protected void onStart() {

//      Here i start the process of connecting the device
//      The onConnectionCompleted method in the other class is called automatically on connection

        CognexHandler cognexhandler = new CognexHandler();
        CognexHandler.createReaderDevice(this); 

}


        public Integer getBatteryLevelPhone() {

        Log.d(TAG, "getBatteryLevelPhone: started");
        
        // Import Views
        TextView tvScanToLogin = (TextView) findViewById(R.id.tvScanToLogin); //Crashes here
        TextView tvWarning = (TextView) findViewById(R.id.tvWarning);

        //  Code that you dont have to worry about
        //

    }

}
代码语言:javascript
复制
public class CognexHandler implements
        ReaderDevice.OnConnectionCompletedListener, ReaderDevice.ReaderDeviceListener,
        ActivityCompat.OnRequestPermissionsResultCallback{

    @Override
    public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {

        MainActivity mainactivity = new MainActivity();
        mainactivity.getBatteryLevelPhone();
    }


}

我确信这是一件很简单的事情,有人可以帮助我:)

EN

回答 3

Stack Overflow用户

发布于 2022-02-17 13:42:06

MainActivity mainactivity =新的Mainactivity();

不能使用new运算符创建活动。

您必须使用意图来启动/创建一个活动。

因此,在此之后,您将没有指向您的活动的指针。

好吧..。我们也没有;-)

票数 1
EN

Stack Overflow用户

发布于 2022-02-17 14:16:53

要添加到黑应用程序的答案,在通过意图启动活动之后,如果它仍然是您当前的活动(!),您可以在CognexHandler中用"getActivity()“调用活动,然后确保它实际上是正确的:

代码语言:javascript
复制
Activity mActivity = getActivity();
if (mActivity instanceof MainActivity) {
      final MainActivity mainActivity = (MainActivity) mActivity;
      mainActivity.getBatteryLevelPhone(); 
}

我建议您使用一种不同的方式与您的活动进行通信,比如侦听器或广播,这可能会给您一个第一步:https://developer.android.com/guide/fragments/communicate

票数 0
EN

Stack Overflow用户

发布于 2022-02-17 15:53:14

试试这个:

代码语言:javascript
复制
 @Override
    public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {

        Activity mainActivity = getActivity();
        mainActivity.getBatteryLevelPhone();
    }

并使您的getBatteryLevelPhone():

代码语言:javascript
复制
public static Integer getBatteryLevelPhone(){

// your code

return yourIntegerResult;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71159182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档