0

我已经阅读了许多关于未调用 onActivityWithResult 的帖子。但是,我仍然无法得到解决方案。

我的程序的思想非常简单:Activity A 调用 Activity B 来获取蓝牙设备的地址和名称。我还没有测试蓝牙功能,因为模拟器不支持蓝牙,我只是调用 get bluetooth device ,它返回一个 null ,表示我没有可用的蓝牙。如果是这样,那么我会返回到活动 A 并祝酒,告诉用户您没有启用蓝牙。

活动一:

OnClickListener myButtonListener2 = new OnClickListener()
{
    public void onClick(View v) 
    {
        startActivityForResult(new Intent("bluetooth_4.pack.BluetoothSettings"), request_bluetooth_code);

        Toast.makeText(MainActivity.this, "Switching to Bluetooth Setting" , Toast.LENGTH_SHORT).show();
    }
};

onActivityCode:

public void onActivituResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == request_bluetooth_code)
    {
        if(resultCode == RESULT_OK)
        {
            Toast.makeText(MainActivity.this, data.getData().toString() , Toast.LENGTH_LONG).show();

            String[] split = data.getData().toString().split("@");
            BluetoothAddress = split[0];
            BluetoothName = split[1];
        }
        else if (resultCode == RESULT_CANCELED)
        {
            Toast.makeText(MainActivity.this, "You cancelled the activity, or you did not have any bluetooth opened" , Toast.LENGTH_SHORT).show();
        }
    }
}

基本上应该发生的事情是因为 resultCode 被取消了,它应该祝酒并且用户会看到它。但是,我在 if 语句上放了一个断点并尝试调试,但它没有命中断点。

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

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.Connect);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);

    button1.setOnClickListener(myButtonListener1);
    button2.setOnClickListener(myButtonListener2);
    button3.setOnClickListener(myButtonListener3);
    button4.setOnClickListener(myButtonListener4);

    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

活动 B:

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

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);

    button1.setOnClickListener(myButtonListener1);
    button1.setOnClickListener(myButtonListener2);

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    this.

    //Get a handle of the local bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    if(mBtAdapter == null)
    {
        Toast.makeText(BluetoothSettings.this, "Your Bluetooth Is Closed, how will this thing work when your bluetooth is closed???" , Toast.LENGTH_LONG).show();

        setResult(RESULT_CANCELED, null);
        noBluetooth = true;
        finish();
    }

    BluetoothSettings someClass = new BluetoothSettings();

    ListView lstView = (ListView) findViewById(R.id.listView1);
    lstView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, deviceListArray));
}

代码应该在活动 B 的 if 语句处停止,因为没有找到适配器。

活动 B 的其他职能:

protected void onDestroy() 
{
    //When we no longer have this activity, we cancel the discovery
    super.onDestroy();
    if (mBtAdapter != null) 
    {
        mBtAdapter.cancelDiscovery();
    }
}

public void onStop() 
{
    super.onStop();
    if(noBluetooth == false)
    {
        BroadcastReceiver localBroadcastReceiver = mReceiver;
        unregisterReceiver(localBroadcastReceiver);
    }
}

这是我的清单文件:

    <activity
        android:name="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="BluetoothSettings" 
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="bluetooth_4.pack.BluetoothSettings" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

我试图调试程序,并且 onActivityResult 没有被调用,有人知道为什么吗?谢谢。

4

1 回答 1

1

我认为这不是您问题的答案,buuuuut 调用finish()不会停止if执行之外的代码。您需要return;在调用后添加 a 以finish()使代码实际停止。

if(mBtAdapter == null)
{
    Toast.makeText(BluetoothSettings.this, "Your Bluetooth Is Closed, how will this thing work when your bluetooth is closed???" , Toast.LENGTH_LONG).show();

    setResult(RESULT_CANCELED, null);
    noBluetooth = true;
    finish();  
    return; //<----- like this. You can also wrap the other code in an else block.
}

BluetoothSettings someClass = new BluetoothSettings();

ListView lstView = (ListView) findViewById(R.id.listView1);
lstView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, deviceListArray));

但是,我刚刚看到您对其进行了编辑并添加了更多代码,并且您的声明中可能存在拼写错误onActivityResult()

 public void onActivituResult(int requestCode, int resultCode, Intent data)
 {...}

那说 activitU 不是 activitY

当您尝试覆盖方法时,使用 @Override注释很有用。当你实际上没有覆盖任何东西时,添加它会警告你。

于 2013-06-15T02:57:58.867 回答