首先,我想我应该描述我的问题: 我想要做一个BluetoothRequest来打开它,但我不想创建一个新的活动。我想保留在MainActivity中并将代码放在子类中。这怎么可能?羽绒服是我自己的想法,如何去做,但它不起作用。onActivityResult not beeing called
我有MainActivity和BluetoothActivity。以下代码来自BluetoothActivity:
public class Bluetooth extends Fragment{
private Context context;
private TextView textView;
private final int REQUEST_ENABLE_BT = 1;
private String BtName;
private String BtAddress;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public Bluetooth(Context context){
//constructor
this.context=context;
textView = new TextView(this.context);
}
public boolean bluetoothUnterstuetz(){
if(mBluetoothAdapter == null) {
//Device doesn't support Bluetooth
textView.setText("Device doesn't support Bluetooth");
}
return true;
}
public void bluetoothActivate(){
if(mBluetoothAdapter.getState()== BluetoothAdapter.STATE_ON){
textView.setText("Bluetooth already on.");
}
if(!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
((Activity) context).setContentView(textView);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
textView.setText(resultCode);
Log.i("bluetooth", "called");
((Activity) context).setContentView(textView);
}
With extends Fragment nothing nothing,logcat is empty。随着扩展活动,应用程序启动后崩溃。 当我测试我的手机上的应用程序,我得到的BluetoothRequest,并可以选择我的答案,但之后的ContentView什么也没有显示,也Logcat保持空
有人知道如何获得结果?
[onActivityResult可能重复不起作用?](http://stackoverflow.com/questions/1984233/onactivityresult-doesnt-work) – 0X0nosugar