2014-02-27 55 views
0

我是新来的。我为我的英语和我的帖子的地方道歉。我有下一个问题“无法恢复活动nullpointerexception”我尝试写NFC标签这是我的代码。无法恢复活动nullpointerexception

package com.example.chips; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.FormatException; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.Ndef; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.Toast; 

@SuppressLint({ "ParserError", "ParserError" }) 
public class MyDialog extends Activity { 

    Button writeBtn; 
    NfcAdapter adapter; 
    PendingIntent pendingIntent; 
    IntentFilter writeTagFilters[]; 
    boolean writeMode; 
    Tag mytag; 
    Context ctx; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.writing_dialog); 
     writeBtn = (Button) findViewById(R.id.writeBtn); 
     ctx = this; 

     writeBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 
        String message = "hola mundo"; 
        if (mytag == null) { 

         Toast.makeText(ctx, "error mytag null", Toast.LENGTH_LONG).show(); 
        } else { 
         write(message, mytag); 
         Toast.makeText(ctx, "writing", Toast.LENGTH_LONG).show(); 
        } 
       } catch (IOException e) { 
        Toast.makeText(ctx, "IO error", Toast.LENGTH_LONG).show(); 
        e.printStackTrace(); 
       } catch (FormatException e) { 
        Toast.makeText(ctx, "error format", Toast.LENGTH_LONG).show(); 
        e.printStackTrace(); 
       } 

      } 
     }); 

     adapter = NfcAdapter.getDefaultAdapter(this); 
     pendingIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
     IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); 
     tagDetected.addCategory(Intent.CATEGORY_DEFAULT); 
     writeTagFilters = new IntentFilter[] { tagDetected }; 

    } 

    private void write(String text, Tag tag) throws IOException, FormatException { 

     NdefRecord[] records = { createRecord(text) }; 
     NdefMessage message = new NdefMessage(records); 
     // Get an instance of Ndef for the tag. 
     Ndef ndef = Ndef.get(tag); 
     // Enable I/O 
     ndef.connect(); 
     // Write the message 
     ndef.writeNdefMessage(message); 
     // Close the connection 
     ndef.close(); 
    } 

    private NdefRecord createRecord(String text) throws UnsupportedEncodingException { 
     String lang = "en"; 
     byte[] textBytes = text.getBytes(); 
     byte[] langBytes = lang.getBytes("US-ASCII"); 
     int langLength = langBytes.length; 
     int textLength = textBytes.length; 
     byte[] payload = new byte[1 + langLength + textLength]; 

     // set status byte (see NDEF spec for actual bits) 
     payload[0] = (byte) langLength; 

     // copy langbytes and textbytes into payload 
     System.arraycopy(langBytes, 0, payload, 1, langLength); 
     System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength); 

     NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, 
       new byte[0], payload); 

     return recordNFC; 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 
     if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
      mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
      Toast.makeText(this, "error intent" + mytag.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     WriteModeOff(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     WriteModeOn(); 
    } 

    private void WriteModeOn() { 
     writeMode = true; 
     adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null); 
    } 

    private void WriteModeOff() { 
     writeMode = false; 
     adapter.disableForegroundDispatch(this); 
    } 

} 

这里我androidmanifest

<activity 
     android:name="com.example.chips.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="MyDialog" 
     android:theme="@android:style/Theme.Dialog" > 
     <intent-filter> 
      <action android:name="com.example.chips"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
     </intent-filter> 
    </activity> 

的logcat:

02-27 05:07:02.386: V/NFC(3687): this device does not have NFC support 
02-27 05:07:02.386: D/AndroidRuntime(3687): Shutting down VM 
02-27 05:07:02.386: W/dalvikvm(3687): threadid=1: thread exiting with uncaught exception (group=0xb3a4dba8) 
02-27 05:07:02.406: E/AndroidRuntime(3687): FATAL EXCEPTION: main 
02-27 05:07:02.406: E/AndroidRuntime(3687): Process: com.example.chips, PID: 3687 
02-27 05:07:02.406: E/AndroidRuntime(3687): java.lang.RuntimeException: Unable to resume activity {...}: java.lang.NullPointerException 
+3

后你在这里logcat的。 –

+0

您的应用程序在第一次加载或从后台进入前台后崩溃了吗? – Vijju

+0

02-27 05:07:02.386 V/NFC(3687):此设备不支持NFC 02-27 05:07:02.386:D/AndroidRuntime(3687):关闭VM 02-27 05: 07:02.386:W/dalvikvm(3687):线程ID = 1:07:02.406:E/AndroidRuntime(3687):致命异常:螺纹与未捕获的异常(组= 0xb3a4dba8) 5月2日至二十七日离开主 02-27 05/07:02.406:E/AndroidRuntime(3687):进程:com.example.chips,PID:3687 02-27 05:07:02.406:E/AndroidRuntime(3687):java.lang.RuntimeException:无法恢复活动{...}:java.lang.NullPointerException – learning134

回答

0
private void WriteModeOn() { 
     writeMode = true; 
if(adapter!=null){ 
     adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null); 
} 
    } 

    private void WriteModeOff() { 
     writeMode = false; 
if(adapter!=null){ 
     adapter.disableForegroundDispatch(this); 
} 
    } 
+0

只回答代码没有太大的帮助,所以尽量多增加一些描述性的答案,所以去和更新 –