java.lang.ClassCastException:android.app.Application-casting

java.lang.ClassCastException:android.app.Application-casting,java,nfc,android,java.lang.class,Java,Nfc,Android,Java.lang.class,java.lang.ClassCastException:android.app.Application无法转换为com.example.project.DataDevice 我的代码: public class Project extends Activity{ private boolean connection = false; public Tag tagFromIntent = null; private Button textRead; private NFCForeground

java.lang.ClassCastException:android.app.Application无法转换为com.example.project.DataDevice

我的代码:

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));
    }
}
}
我的舱单代码:

<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>
数据设备类:

public class DataDevice extends Application
{
    private Tag currentTag;

    public void setCurrentTag(Tag currentTag) {
        this.currentTag = currentTag;
    }

    public Tag getCurrentTag() {
        return currentTag;
    }

    //(...)
}
我在stackOverflow上寻找答案,没有任何帮助。 有人知道发生了什么吗?
DataDevice和NFCUtilForeground(在其他应用程序中)运行良好。

清单文件中
application
标记中的
android:name
属性应指向您的
DataDevice
类。比如:

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" 
android:name="your.package.DataDevice">

.........
..........
</application>

.........
..........

显示DataDevice类DataDevice=(DataDevice)getApplication()的开头;是线路引起了问题。看看,这对我不管用。我正在尝试从服务将getApplication()强制转换为活动的类。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" 
android:name="your.package.DataDevice">

.........
..........
</application>