2

I'm making an android app with 2 activities and a Java class that reads/writes RFID data using NFC.

I'm using the enableReaderMode() method to enable reader/writer mode in the mainActivity, which then calls onTagDiscovered in a separate java class. Once in the onTagDiscovered method, I'm calling mainActivity.startNewActivity() to open a the second activity. I know that I'm reaching startNewActivity() because of a log message, but the activity is not actually starting. The app doesn't crash, and I'm not getting any errors, it just isn't starting. I've tried placing a button in the mainActivity and opening that way, and it works fine. It just doesn't work when I call the method from encode.java.

**I know that it is not necessary to open the activity from Encode.java, and that I could directly code this into MainActivity, but I'm preparing to do something more complex where I will need to open from Encode.java, and I want to test that the activity will open at all.

Here's the code

Encode.java

public class Encode implements NfcAdapter.ReaderCallback {
    MainActivity mainActivity = new MainActivity(); 

    public void onTagDiscovered(Tag tag) {
        Log.i(TAG, "New tag discovered"); 
        mainActivity.startNewActivity(); 
    }

MainActivity

 public void startNewActivity() {
    Log.v(TAG, "in startNewActivity");
    Intent intent = new Intent(this, Success.class);
    startActivity(intent);
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.project" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.NFC"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/xband"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech" />

        </activity>
        <activity android:name=".Success" >

        </activity>
    </application>

</manifest>

Success.java

package com.project;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Success extends Activity {
    private static final String TAG = "encode";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.success_activity);
        Log.v(TAG, "in success activity");
    }


}

I tried adding the following code and then got this error message: Encode.java

public void onTagDiscovered(Tag tag) {
        Log.i(TAG, "New Tag Discovered");
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
           @Override
            public void run() {
               mainActivity.startNewActivity();
           }
        });
}


02-20 11:52:27.739  22088-22088/com.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.project, PID: 22088
    java.lang.NullPointerException
            at android.app.Activity.startActivityForResult(Activity.java:3474)
            at android.app.Activity.startActivityForResult(Activity.java:3435)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
            at android.app.Activity.startActivity(Activity.java:3677)
            at android.app.Activity.startActivity(Activity.java:3645)
            at com.project.MainActivity.startNewActivity(MainActivity.java:110)
            at com.project.encoding.Encode$1.run(Encode.java:331)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
mofitty
  • 111
  • 1
  • 3
  • 10
  • 4
    is there any reason why one is called `mainActivity.startNewActivity();` and the other is called `startNewActivity2()`? – EpicPandaForce Feb 19 '15 at 20:45
  • meant to take the 2 out. That's not in the real code. Thanks! – mofitty Feb 19 '15 at 20:50
  • in the worst case scenario, try the following code: `Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { public void run() { mainActivity.startNewActivity(); } );` – EpicPandaForce Feb 19 '15 at 20:51
  • try this in you startNewActivity method: Intent intent = new Intent(mainActivity.this, Success.class); – zkminusck Feb 19 '15 at 20:55
  • @zkminusck that didn't seem to fix it either. – mofitty Feb 19 '15 at 20:59
  • we might need more code, and maybe stack trace/logs – EpicPandaForce Feb 19 '15 at 20:59
  • Can you post `Success.java` and your `AndroidManifest.xml` files – Chris Thompson Feb 19 '15 at 21:01
  • you need to post (and read) your log. – njzk2 Feb 19 '15 at 21:01
  • @njzk2 I'm reading the log android logcat, but I'm not seeing any error messages. Is there something else I should be looking at? – mofitty Feb 19 '15 at 21:12
  • @mofitty: anything below `"in startNewActivity"` – njzk2 Feb 19 '15 at 21:20
  • @njzk2 there isn't anything below "in startNewActivity" I'm not getting any error messages or warnings, no crashes, nothing – mofitty Feb 19 '15 at 21:25
  • Out of curiosity, have you tried doing all the `startActivity` work inside `onTagDiscovered` method itself? I'm wondering if there is something weird with your reference to `mainActivity` within Encode.java – Eric Farraro Feb 19 '15 at 21:52
  • @EricFarraro I don't know how to open an activity from a java class that isn't an activity itself. If you know how to do it I would be happy to try! I don't think there's an issue reaching the 'MainActivity' though because the log message there is printing. – mofitty Feb 20 '15 at 14:07
  • @EpicPandaForce I tried using the handler and it threw a nullpointerexception. I've posted the code and the error in the original question. Any ideas? – mofitty Feb 20 '15 at 16:56
  • You can open an Activity with any context, including the ApplicationContext (you can have it as a static variable, accessible anywhere) as long as you set the flag for FLAG_ACTIVITY_NEW_TASK. – EpicPandaForce Feb 20 '15 at 17:03

2 Answers2

3

Someone at work was able to help me figure this out. This wasn't working because I was creating a new instance of the MainActivity in Encode, instead of referencing the existing MainActivity.

In MainActivity we added:

private static MainActivity _instance = null; 

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            encode = new Encode();

            enableReaderMode();

           MainActivity._instance = this;
        }

        public static MainActivity getInstance() {
            return _instance;
        }

and in Encode we called the startNewActivity method by calling:

public void onTagDiscovered(Tag tag) {
    Log.i(TAG, "New Tag Discovered");

    MainActivity.getInstance().startNewActivity();

});

so that now Encode is using the existing instance of MainActivity

mofitty
  • 111
  • 1
  • 3
  • 10
1

Instead of: Intent intent = new Intent(this, Success.class);
Try: Intent intent = new Intent(getApplicationContext(), Success.class);

That worked for me one time cause it "Return the context of the single, global Application object of the current process. "

Eric
  • 11
  • 1