2016-08-02 66 views
-3

我想從一個活動發送一個字符串從數字EditText到另一個。但是當我點擊保存我的apk崩潰。這裏是代碼和logcat。Android發送字符串時崩潰

設置

public class settings extends Activity { 

    Button btnSave; 
    TextView text; 

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

     btnSave = (Button)findViewById(R.id.save); 
     final EditText passkey = (EditText)findViewById(R.id.editText); 
     text=(TextView)findViewById(R.id.textView3); 


     btnSave.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String pass = passkey.getText().toString(); 

       Intent myIntent = new Intent(settings.this, unlock.class); 
       myIntent.putExtra("String", pass); 
       startActivity(myIntent); 
      } 
     }); 
    } 
} 

解鎖

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_unlock); 

     String newString; 
     if (savedInstanceState == null) { 
      Bundle extras = getIntent().getExtras(); 
      if(extras == null) { 
       newString= null; 
      } else { 
       newString= extras.getString("String"); 
      } 
     } else { 
      newString= (String) savedInstanceState.getSerializable("String"); 
     } 

的onResume()方法。

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

     Intent intent = getIntent(); 

     address = intent.getStringExtra(DeviceList.EXTRA_ADDRESS); 

     BluetoothDevice device = btAdapter.getRemoteDevice(address); 

     try { 
      btSocket = createBluetoothSocket(device); 
     } catch (IOException e) { 
      Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show(); 
     } 
     try 
     { 
      btSocket.connect(); 
     } catch (IOException e) { 
      try 
      { 
       btSocket.close(); 
      } catch (IOException e2) 
      { 
       // 
      } 
     } 
     mConnectedThread = new ConnectedThread(btSocket); 
     mConnectedThread.start(); 

     mConnectedThread.write("x"); 
    } 

設備列表

public class DeviceList extends AppCompatActivity { 

    Button btnPaired; 
    ListView devicelist; 
    private BluetoothAdapter myBluetooth = null; 
    private Set<BluetoothDevice> pairedDevices; 
    public static String EXTRA_ADDRESS = "device_address"; 

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

     btnPaired = (Button)findViewById(R.id.button); 
     devicelist = (ListView)findViewById(R.id.listView); 

     myBluetooth = BluetoothAdapter.getDefaultAdapter(); 
     if(myBluetooth == null) 
     { 

      Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show(); 

      finish(); 
     } 
     else 
     { 
      if (myBluetooth.isEnabled()) 
      { } 
      else 
      { 

       Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(turnBTon,1); 
      } 
     } 
     btnPaired.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       pairedDevicesList(); 
      } 
     }); 
    } 

    private void pairedDevicesList() 
    { 
     pairedDevices = myBluetooth.getBondedDevices(); 
     ArrayList list = new ArrayList(); 

     if (pairedDevices.size()>0) 
     { 
      for(BluetoothDevice bt : pairedDevices) 
      { 
       list.add(bt.getName() + "\n" + bt.getAddress()); 
      } 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show(); 
     } 

     final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); 
     devicelist.setAdapter(adapter); 
     devicelist.setOnItemClickListener(myListClickListener); 
    } 
    private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() 
    { 
     public void onItemClick (AdapterView av, View v, int arg2, long arg3) 
     { 
      String info = ((TextView) v).getText().toString(); 
      String address = info.substring(info.length() - 17); 

      Intent i = new Intent(DeviceList.this, unlock.class); 

      i.putExtra(EXTRA_ADDRESS, address); 
      startActivity(i); 
     } 
    }; 

} 





  
+0

顯示你的'onResume()'? –

+0

我發佈了它,我希望它可以幫助 –

+0

你在做什麼onResume(); –

回答

0

現在正在發生的事情,你是從two positionsdifferent values一個與device address和一個與edit text值開Single class

所以當你傳遞editext值設備地址爲空,你的應用程序崩潰來處理這個添加驗證。 if(address != null)然後連接,否則不。

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

     Intent intent = getIntent(); 
     address = intent.getStringExtra(DeviceList.EXTRA_ADDRESS); 

     if(address != null){ 
     BluetoothDevice device = btAdapter.getRemoteDevice(address); 

     try { 
      btSocket = createBluetoothSocket(device); 
     } catch (IOException e) { 
      Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show(); 
     } 
     try 
     { 
      btSocket.connect(); 
     } catch (IOException e) { 
      try 
      { 
       btSocket.close(); 
      } catch (IOException e2) 
      { 
      } 
     } 
     mConnectedThread = new ConnectedThread(btSocket); 
     mConnectedThread.start(); 
     mConnectedThread.write("x"); 
    } 
} 
+0

它現在的作品!謝謝 –

+0

@ H.Stefek歡迎您接下來提供完整的info.happy編碼 –

+0

現在,當我從設置返回解鎖時,無論我按哪個按鈕,它都會崩潰 –

-1

您需要使用此行:

newString =extras.getString("String", null); 

,而不是這一行:

newString= extras.getString("String"); 
+0

仍然同樣崩潰 –

0

onResume()你正在做

Intent intent = getIntent(); 

     address = intent.getStringExtra(DeviceList.EXTRA_ADDRESS); 

     BluetoothDevice device = btAdapter.getRemoteDevice(address); 

你什麼時候通過了關鍵的意圖DeviceList.EXTRA_ADDRESS,這裏是問題。之後,您將通過該空地址找到remoteDevice。

+0

你解釋日誌以及解決方案? –

+0

我有3個活動,4個,但一個是加載屏幕,不計算它,一個是設備列表,它發現arduino藍牙設備,並連接到它,然後打開解鎖活動 –

+0

引起:java.lang.IllegalArgumentException:null不是一個有效的藍牙地址。 – Aditi