H.Stefek H.Stefek - 4 months ago 61
Android Question

Android crashes when sends string

I want to send a string from a numeric

EditText
from one activity to another. But when I click save my apk crashes. Here is the code and logcat.

Settings

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);
}
});
}
}


Unlock

@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() method.

@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");
}


Device list

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);
}
};

}


AND here is logcat too

08-02 11:24:56.971 7305-7305/integraone.integra E/AndroidRuntime: FATAL EXCEPTION: main
Process: integraone.integra, PID: 7305
java.lang.RuntimeException: Unable to resume activity {integraone.integra/integraone.integra.unlock}: java.lang.IllegalArgumentException: null is not a valid Bluetooth address
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3224)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.IllegalArgumentException: null is not a valid Bluetooth address
at android.bluetooth.BluetoothDevice.<init>(BluetoothDevice.java:681)
at android.bluetooth.BluetoothAdapter.getRemoteDevice(BluetoothAdapter.java:481)
at integraone.integra.unlock.onResume(unlock.java:161)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1331)
at android.app.Activity.performResume(Activity.java:6044)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3257) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2479) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)







 

Answer

Now what is happening you are opening Single class from two positions with different values one with device address and one with edit text value.

so when you pass editext value device address is null and your app crash to handle this add validations. if(address != null) then connect otherwise not.

   @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");
    }
}
Comments