skip to Main Content

I have two classes, the MainActivity Class and a class (CognexHandler) on which i connect a USB Device .

When the Connection of the Device is completed, i want to call a method in the MainActivity Class which contains TextViwes to the activity_main.xml File.

On the initialisation of the first TextView –

TextView tvScanToLogin = (TextView) findViewById(R.id.tvScanToLogin);

the Programm crashes with the following error code:

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)

Here the Code of the Classes:

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


}

Im sure itΒ΄s something really simple and that someone can help me πŸ™‚

3

Answers


  1. MainActivity mainactivity = new Mainactivity();

    You can not use the new operator to create an Activity.

    You have to use an Intent to start/create an Activity.

    So after that you will not have a pointer to your activity.

    Well… we dont have either πŸ˜‰

    Login or Signup to reply.
  2. To add to blackapps answer, after having started the activity via the intent and if it’s still your current activity (!), in CognexHandler you can call the active activity with "getActivity()" and then make sure it’s actually the correct one:

    Activity mActivity = getActivity();
    if (mActivity instanceof MainActivity) {
          final MainActivity mainActivity = (MainActivity) mActivity;
          mainActivity.getBatteryLevelPhone(); 
    }
    

    I would recommend using a different way to communicate with your Activity, like Listeners or Broadcasts, this might give you a first start:
    https://developer.android.com/guide/fragments/communicate

    Login or Signup to reply.
  3. try this:

     @Override
        public void onConnectionCompleted(ReaderDevice readerDevice, Throwable throwable) {
    
            Activity mainActivity = getActivity();
            mainActivity.getBatteryLevelPhone();
        }
    

    And Make your getBatteryLevelPhone() to:

    public static Integer getBatteryLevelPhone(){
    
    // your code
    
    return yourIntegerResult;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search