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)