我有两个类,MainActivity类和一个连接USB设备的类(CognexHandler)。
当设备连接完成后,我想在MainActivity类中调用一个方法,该方法包含TextViwes到activity_main.xml文件。
关于第一个TextView的初始化-
TextView tvScanToLogin = (TextView) findViewById(R.id.tvScanToLogin);该程序使用以下错误代码崩溃:
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)在这里,课程代码:
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
//
}
}public class CognexHandler implements
ReaderDevice.OnConnectionCompletedListener, ReaderDevice.ReaderDeviceListener,
ActivityCompat.OnRequestPermissionsResultCallback{
@Override
public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {
MainActivity mainactivity = new MainActivity();
mainactivity.getBatteryLevelPhone();
}
}我确信这是一件很简单的事情,有人可以帮助我:)
发布于 2022-02-17 13:42:06
MainActivity mainactivity =新的Mainactivity();
不能使用new运算符创建活动。
您必须使用意图来启动/创建一个活动。
因此,在此之后,您将没有指向您的活动的指针。
好吧..。我们也没有;-)
发布于 2022-02-17 14:16:53
要添加到黑应用程序的答案,在通过意图启动活动之后,如果它仍然是您当前的活动(!),您可以在CognexHandler中用"getActivity()“调用活动,然后确保它实际上是正确的:
Activity mActivity = getActivity();
if (mActivity instanceof MainActivity) {
final MainActivity mainActivity = (MainActivity) mActivity;
mainActivity.getBatteryLevelPhone();
}我建议您使用一种不同的方式与您的活动进行通信,比如侦听器或广播,这可能会给您一个第一步:https://developer.android.com/guide/fragments/communicate。
发布于 2022-02-17 15:53:14
试试这个:
@Override
public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {
Activity mainActivity = getActivity();
mainActivity.getBatteryLevelPhone();
}并使您的getBatteryLevelPhone():
public static Integer getBatteryLevelPhone(){
// your code
return yourIntegerResult;
}https://stackoverflow.com/questions/71159182
复制相似问题