我是新来的。我为我的英语和我的职位感到抱歉。我有下一个问题“无法恢复活动nullpointerexception”我尝试在NFC标签上写这是我的代码。
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
答案 0 :(得分: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);
}
}