title: java.lang.ClassCastException: android.app.Application - casting
body: <p>java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.DataDevice</p>
  
  <p>My code:</p>
  
  <pre><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));
      }
  }
  }
  </code></pre>
  
  <p>My manifest code:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.project"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" /&gt;
  
  &lt;uses-permission android:name="android.permission.NFC" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
  &lt;uses-feature
      android:name="android.hardware.nfc"
      android:required="true" /&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" 
      android:name="android.app.Application"&gt; 
      &lt;activity
          android:name=".StartActivity"
          android:label="@string/title_activity_main" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;activity 
          android:screenOrientation="portrait" 
          android:name=".Project" 
          android:label="@string/title_activity_main" /&gt;
  
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>My error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>DataDevice class:</p>
  
  <pre><code>public class DataDevice extends Application
  {
      private Tag currentTag;
  
      public void setCurrentTag(Tag currentTag) {
          this.currentTag = currentTag;
      }
  
      public Tag getCurrentTag() {
          return currentTag;
      }
  
      //(...)
  }
  </code></pre>
  
  <p>I looked for answer on stackOverflow, nothing helped.
  Anyone knows what's going on? 
  DataDevice and NFCUtilForeground works good (in other applications).</p>