2016-03-28 14 views
-2
I have made an NFC tag writing app that has various options such as writing text,URL,toggle Wifi, Business Card etc. 
The problem i am facing is when i click the write button after entering text. The app terminates on itself. 
May be i am calling the wrong method. 

ユーザがリストからURLを書き込むことを選択した場合に必要です。その後、Write URLアクティビティが開始され、Writeボタンを押すとWriteタグが表示されます。今、私はタグを書くボタンを押しています。アプリは「残念ながらアプリは停止しています」と言っています。NFCタグをテキストまたはURLで書き込むことができない

ListDisplay

package com.example.jordjr.projectwork; 

import android.content.Intent; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class ListDisplay extends Activity implements AdapterView.OnItemClickListener { 
    // Array of strings... 

    ListView listview; 
    String[] mobileArray = {"Text", "URL", "Email", "Phone Number", "Wifi", "Business Card"}; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray); 

     listview = (ListView) findViewById(R.id.mobile_list); 
     listview.setAdapter(adapter); 

     listview.setOnItemClickListener(this); 
      } 


    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 



     String item = ((TextView) view).getText().toString(); 

     if(item.equalsIgnoreCase("text")){ 
      Intent intent=new Intent (this,WriteUrl.class); 
      startActivity(intent); 
     } 
     else 
     if(item.equalsIgnoreCase("Phone Number")){ 
      Intent intent=new Intent (this,PhoneNumber.class); 
      startActivity(intent); 

     } 
     else 
     if(item.equalsIgnoreCase("URL")) { 
      Intent intent = new Intent(this, WriteUrl.class); 
      startActivity(intent); 
     } 
      else 
      if(item.equalsIgnoreCase("Wifi")) { 
       Intent intent = new Intent(this,Wifi.class); 
       startActivity(intent); 
      } 
      else 
      if(item.equalsIgnoreCase("Email")) { 
       Intent intent = new Intent(this,WriteEmail.class); 
       startActivity(intent); 
      } 
      else 
      if(item.equalsIgnoreCase("Business Card")) { 
       Intent intent = new Intent(this, BusinessCard.class); 
       startActivity(intent); 
      } 


    } 

    } 

書き込みURL パッケージcom.example.jordjr.projectwork。

import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.Uri; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.nfc.Tag; 
import android.nfc.tech.Ndef; 
import android.nfc.tech.NdefFormatable; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class WriteUrl extends AppCompatActivity { 



    NfcAdapter nfcAdapter; 
    Button button; 
    EditText txtTagContent; 
    int x=0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_write_url); 

     nfcAdapter=NfcAdapter.getDefaultAdapter(this); 
     button = (Button)findViewById(R.id.button); 
     txtTagContent = (EditText)findViewById(R.id.txtTagContent); 

    } 


    @Override 
    protected void onResume() { 
     super.onResume(); 
     enableForegroundDispatchSystem(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 

     disableForegroundDispatchSystem(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 



    private void enableForegroundDispatchSystem() 
    { 
     Intent intent=new Intent(this, MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

     IntentFilter[] intentFilters = new IntentFilter[] {}; 

     nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null); 
    } 


    private void disableForegroundDispatchSystem() 
    { 
     nfcAdapter.disableForegroundDispatch(this); 

    } 

    private void formatTag(Tag tag,NdefMessage ndefMessage) 
    { 
     try 
     { 
      NdefFormatable ndefFormatable=NdefFormatable.get(tag); 

      if(ndefFormatable == null) 
      { 
       Toast.makeText(this, "This Tag is Not ndef Formatable!", Toast.LENGTH_SHORT).show(); 
      } 
      ndefFormatable.connect(); 
      ndefFormatable.format(ndefMessage); 
      ndefFormatable.close(); 
      Toast.makeText(this, "Tag written", Toast.LENGTH_SHORT).show(); 
     } 
     catch (Exception e) 
     { 
      Log.e("formatTag", e.getMessage()); 

     } 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     if (x == 1) { 
      if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) { 
       Toast.makeText(this, "NfcIntent!", Toast.LENGTH_SHORT).show(); 


       Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
       NdefMessage ndefMessage = createNdefMessage(txtTagContent.getText() + ""); 

       writeNdefMessage(tag, ndefMessage); 

      } 
      x=0; 
     } 
    } 
    private void writeNdefMessage(Tag tag, NdefMessage ndefMessage) 
    { 
     try 
     { 
      if(tag == null) 
      { 
       Toast.makeText(this, "Tag object cannot be null", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      Ndef ndef = Ndef.get(tag); 

      if(ndef == null) 
      { 
       //formats tag with the ndef format and writes the message 
       formatTag(tag, ndefMessage); 
      } 

      else 
      { 
       ndef.connect(); 

       if(!ndef.isWritable()) 
       { 
        Toast.makeText(this, "Tag is not writable!", Toast.LENGTH_SHORT).show(); 
        ndef.close(); 
        return; 
       } 

       ndef.writeNdefMessage(ndefMessage); 
       ndef.close(); 

       Toast.makeText(this, "Tag written", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     catch(Exception e) 
     { 

      Log.e("writeNdefMessage", e.getMessage()); 
     } 
    } 

    private NdefMessage createNdefMessage(String content) 
    { 

     NdefRecord ndefRecord = createUrlRecord(content); 

     NdefMessage ndefMessage=new NdefMessage(new NdefRecord[] {ndefRecord}); 

     return ndefMessage; 
    } 

    private NdefRecord createUrlRecord(String content) 
    { 
     try { 
      Uri uri = Uri.parse("http://" + content); 
      NdefRecord ndefRecord = NdefRecord.createUri(uri); 
      return ndefRecord; 
     } 
     catch(Exception e) 
     { 
      Log.e("create Ndef Record", e.getMessage()); 
      return null; 
     } 
    } 

    public void writeUri(View view) { 
     Toast.makeText(this, "Bring forth thy nfc tag", Toast.LENGTH_SHORT).show(); 
     x=1; 
    } 
} 

xmlファイル

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingRight="16dp" 
android:paddingTop="16dp" 
android:paddingBottom="16dp" 
android:paddingLeft="16dp" 
tools:context="com.example.jordjr.projectwork.WriteUrl"> 



    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:text="WRITE URL" 
     android:checked="true" 
     android:onClick="writeUri" 
     /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/txtTagContent" 
     android:layout_gravity="top" 
     android:height="40dp" 
     android:width="320dp" 
     android:text="Enter Here" 
    enter code here 

     /> 






</RelativeLayout> 
+0

アプリが強制的に強制終了される原因は何ですか? –

答えて

0

私がコメントを投稿することができません。あなたはマニフェストのすべての活動を宣言しましたか? Like:

<activity 
      android:name=".WriteUrl"> 
    </activity> 
    <activity 
      android:name=".PhoneNumber"> 
    </activity> 

...などですか?

ボタン「書き込み」を押すと、新しいアクティビティが開始されます。その活動が宣言されていない場合、アプリは "残念なことに、アプリが停止されました"というメッセージで壊れてしまいます。

関連する問題