0

I'd like to know how that can be done, I'm further explaining my situation:

I'm calling a custom made class this way from an Activity outside of the library.

MifareReaderAndWriter mraw=new MifareReaderAndWriter(this);
mraw.readSector();

This is my MifareReaderAndWriter class constructor and its method readSector():

 public MifareReaderAndWriter(Context context)
    {
        this.context=context;
    }



   public void readSector()
    {
        Intent intent=new Intent(context,com.example.mifarereaderandwriter.ReadTag.class);
        startActivity(intent);

    }

When trying to execute first instruction in readSector I'm getting the following error:

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

This is an error that usually happens if the class is not defined in AndroidManifest.xml, so in the corresponding one on the project I attempt to define it this way:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mifarereaderandwriter">

    
    <?xml version="1.0" encoding="utf-8"?>
   
        <uses-permission android:name="android.permission.NFC" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <uses-feature
            android:name="android.hardware.nfc"
            android:required="true" />

        <application
            android:name="com.example.mifarereaderandwriter.Common"
            android:allowBackup="false"
            android:requestLegacyExternalStorage="true"
            android:label="@string/app_name"
           >
         
            <activity
                android:name="com.example.mifarereaderandwriter.ReadTag"
                android:configChanges="keyboardHidden|orientation|screenSize"
                android:label="@string/title_activity_read_tag" >
            </activity>


        </application>

    </manifest>

But when I try to compile the project I get an error which in English translates to:

The destiny of processing instructtion that matches with "[xX][mM][lL]" is not allowed.

Which, to be honest, I don't have the slightest idea of what can mean.

Trying to define the class in the manifest of the class that calls the library at least compiles the code, but it yields the same error about null objet reference. On top of that, a library is not supposed to work that way.

What am I doing wrong?

Due to @Cyb3rKo suggestion, I'm showing what I've done, I'm trying to call start Activity this way:

  ComponentName cn = new ComponentName(context,com.example.mifarereaderandwriter.ReadTag.class);
        Intent intent=new Intent().setComponent(cn);
        startActivity(intent);

But keep on getting this error now:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()

I've also managed to change the manifest for this one who does compile:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mifarereaderandwriter"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />

    <application>
        <activity
            android:name="com.example.mifarereaderandwriter.ReadTag"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/title_activity_read_tag" >
        </activity>
    </application>
</manifest>

What am I missing?

mylket
  • 147
  • 7
  • Maybe you could also include the name of the exception, so that we can identify the error message (the name stands in front of the message) – Cyb3rKo Sep 06 '20 at 16:01
  • @Cyb3rKo, exception is now: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference – mylket Sep 06 '20 at 16:02
  • Oh ok, I meant the other error you mentioned ("The destiny of processing instructtion..."). Then maybe this can help you: https://stackoverflow.com/questions/31059390/android-null-pointer-exception-when-calling-new-intent/31059575 – Cyb3rKo Sep 06 '20 at 16:06
  • Maybe also check if you passed and used the context correctly. – Cyb3rKo Sep 06 '20 at 16:06

1 Answers1

1

I tried to start an activity in another app, but the solution is the same:
You don't use the constructor, but you add a Component via the .setComponent method.

For my source and some example code see here:
https://stackoverflow.com/a/25606239/9077356

Cyb3rKo
  • 413
  • 7
  • 22
  • thanks for your answer, but not working unless I've done something wrong I'm editing answer to show what I have done. – mylket Sep 06 '20 at 15:58