java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.DataDevice
My code:
public class Project extends Activity{
private boolean connection = false;
public Tag tagFromIntent = null;
private Button textRead;
private NFCForegroundUtil nfcForegroundUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
nfcForegroundUtil = new NFCForegroundUtil(this);
this.textRead= (Button) findViewById(R.id.button2);
initListeners();
}
private void initListeners() {
textRead.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (connection == true)
{
DataDevice dataDevice = (DataDevice) getApplication();
dataDevice.setCurrentTag(tagFromIntent);
IsoDep nfca = IsoDep.get(dataDevice.getCurrentTag());
try
{
byte[] read= new byte[] { 0x00};
byte[] ans = null;
nfca.setTimeout(2000);
nfca.connect();
nfca.setTimeout(2000);
if (nfca.isConnected())
{
nfca.setTimeout(2000);
ans = nfca.transceive(read);
try
{
Thread.sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
nfca.close();
String textRead = HexBin.encode(ans);
}
catch (IOException e)
{
Log.i("A", "IOException is: " + e.getMessage());
e.printStackTrace();
}
if (nfca.isConnected())
{
try
{
nfca.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
});
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
action = intent.getAction();
tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
connection = true;
}
public void onPause()
{
super.onPause();
nfcForegroundUtil.disableForeground();
}
public void onResume()
{
super.onResume();
nfcForegroundUtil.enableForeground();
if (!nfcForegroundUtil.getNfc().isEnabled())
{
Toast.makeText(
getApplicationContext(),
"Please activate NFC and press Back to return to the application!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
}
My manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<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:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="android.app.Application">
<activity
android:name=".StartActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".Project"
android:label="@string/title_activity_main" />
</application>
My error:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.Project.DataDevice
at com.example.project.Project$1.onClick(Project.java:67)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
DataDevice class:
public class DataDevice extends Application
{
private Tag currentTag;
public void setCurrentTag(Tag currentTag) {
this.currentTag = currentTag;
}
public Tag getCurrentTag() {
return currentTag;
}
//(...)
}
I looked for answer on stackOverflow, nothing helped. Anyone knows what's going on? DataDevice and NFCUtilForeground works good (in other applications).
The android:name
attribute in application
tag in your manifest file should point to your DataDevice
class. Like:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="your.package.DataDevice">
.........
..........
</application>