我有兩個類,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();
}
}
我確定這真的很簡單,有人可以幫助我:)
uj5u.com熱心網友回復:
MainActivity mainactivity = new Mainactivity();
你不能使用new運算子來創建一個Activity。
您必須使用 Intent 來啟動/創建 Activity。
所以在那之后你將沒有指向你的活動的指標。
嗯......我們也沒有;-)
uj5u.com熱心網友回復:
要添加到 blackapps 的答案,在通過意圖啟動活動之后,如果它仍然是您當前的活動 (!),在 CognexHandler 中,您可以使用“getActivity()”呼叫活動活動,然后確保它實際上是正確的活動:
Activity mActivity = getActivity();
if (mActivity instanceof MainActivity) {
final MainActivity mainActivity = (MainActivity) mActivity;
mainActivity.getBatteryLevelPhone();
}
我建議使用不同的方式與您的 Activity 進行通信,例如 Listeners 或 Broadcasts,這可能會給您一個第一個開始: https ://developer.android.com/guide/fragments/communicate
uj5u.com熱心網友回復:
嘗試這個:
@Override
public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {
Activity mainActivity = getActivity();
mainActivity.getBatteryLevelPhone();
}
并使您的 getBatteryLevelPhone() 為:
public static Integer getBatteryLevelPhone(){
// your code
return yourIntegerResult;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428328.html
上一篇:AndroidStudio不允許我使用repeatOnLifecycle
下一篇:AlarmManager過早觸發
