我正在開發一個樣本Android應用程序有兩個活動NullPointerException異常:嘗試在空對象引用
- 一個登錄頁面
- 的活動寫入調用虛擬方法enableForegroundDispatch(...) NDEF消息到NFC標籤。
點擊登錄按鈕後,出現NullPointerException。這裏是整個日誌:
12-08 11:57:32.510: E/AndroidRuntime(1162): FATAL EXCEPTION: main
12-08 11:57:32.510: E/AndroidRuntime(1162): Process: com.example.garageconnect, PID: 1162
12-08 11:57:32.510: E/AndroidRuntime(1162): java.lang.RuntimeException: Unable to resume activity {com.example.garageconnect/com.example.garageconnect.WriteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.os.Looper.loop(Looper.java:148)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.main(ActivityThread.java:5417)
12-08 11:57:32.510: E/AndroidRuntime(1162): at java.lang.reflect.Method.invoke(Native Method)
12-08 11:57:32.510: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-08 11:57:32.510: E/AndroidRuntime(1162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-08 11:57:32.510: E/AndroidRuntime(1162): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
12-08 11:57:32.510: E/AndroidRuntime(1162): at com.example.garageconnect.WriteActivity.WriteModeOn(WriteActivity.java:134)
12-08 11:57:32.510: E/AndroidRuntime(1162): at com.example.garageconnect.WriteActivity.onResume(WriteActivity.java:129)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.Activity.performResume(Activity.java:6312)
12-08 11:57:32.510: E/AndroidRuntime(1162): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
12-08 11:57:32.510: E/AndroidRuntime(1162): ... 10 more
MainActivity.java
public class MainActivity extends Activity {
EditText t1,t2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username=t1.getText().toString(),pwd=t2.getText().toString();
if(username.equals("Siva") && pwd.equals("pwd123")){
Intent i = new Intent(getBaseContext(), WriteActivity.class);
startActivity(i);
}
}
});
}
}
WriteActivity.java
public class WriteActivity extends Activity {
boolean writeMode;
NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
EditText t1,t2;
Button b1;
String nfcmsg;
Tag tag;
Context ctx;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.write_activity);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
ctx=this;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if(tag==null){
Toast.makeText(ctx, "No tag found", Toast.LENGTH_LONG).show();
}
else{
write(t1.getText().toString(),t2.getText().toString(),tag);
Toast.makeText(ctx, "Done Successfully", Toast.LENGTH_LONG).show();
}
}
catch (IOException e) {
Toast.makeText(ctx, "Error in Writing", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
catch (FormatException e) {
Toast.makeText(ctx, "Error in Writing FF" , 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 locid,String secid, Tag tag) throws IOException, FormatException {
//Code that calls createRecord method to create NDEFRecord and write NDEF message to NFC tag
}
private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
//Code to create NDEF record and returns the record to write method
}
@Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(this, "Tag Detected. Now Write!", 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);
}
}
在'onPause(..)'和'onResume(....)上添加'if(adapter!= null)'' –
哇!謝謝,但我想知道原因。起初,我的應用程序只有WriteActivity。那時候,它正在工作,那麼爲什麼這次又是例外呢? –