我刚接触到这里的新手,正在研究我的第一个项目,在我为活动添加新按钮并且应用程序无法启动之前,这一切都很顺利! (在平板电脑上)
我将所有更改恢复到原来的状态,但我仍然遇到这个问题,此代码在此之前工作正常,我不能为我的生活解决原因!
我的应用程序有两个活动,启动活动是蓝牙设置页面(或者它将在完成时),它具有打开/关闭蓝牙,搜索设备,列出配对设备和按钮的按钮切换到第二个活动,该活动是一个屏幕,其中一些按钮打开以执行一些尚未完成的操作,一个后退按钮返回到第一个活动。
当我从第一个活动改为第二个活动时,我的问题就出现了,我得到了"不幸的是,bluetoothtest停止了#34;。
发生了logcat错误:
07-07 16:01:38.757: D/AndroidRuntime(3595): Shutting down VM
07-07 16:01:38.757: W/dalvikvm(3595): threadid=1: thread exiting with uncaught exception (group=0x40d89930)
07-07 16:01:38.757: E/AndroidRuntime(3595): FATAL EXCEPTION: main
07-07 16:01:38.757: E/AndroidRuntime(3595): java.lang.RuntimeException: Unable to destroy activity {com.javacodegeeks.android.bluetoothtest/sjtech.rompa.wifi.Bluetoothsetup}:java.lang.IllegalArgumentException: Receiver not registered: sjtech.rompa.wifi.Bluetoothsetup$1@413a0858
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3586)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3605)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread.access$1300(ActivityThread.java:158)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.os.Looper.loop(Looper.java:176)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread.main(ActivityThread.java:5365)
07-07 16:01:38.757: E/AndroidRuntime(3595): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 16:01:38.757: E/AndroidRuntime(3595): at java.lang.reflect.Method.invoke(Method.java:511)
07-07 16:01:38.757: E/AndroidRuntime(3595): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-07 16:01:38.757: E/AndroidRuntime(3595): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-07 16:01:38.757: E/AndroidRuntime(3595): at dalvik.system.NativeStart.main(Native Method)
07-07 16:01:38.757: E/AndroidRuntime(3595): Caused by: java.lang.IllegalArgumentException: Receiver not registered: sjtech.rompa.wifi.Bluetoothsetup$1@413a0858
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:662)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1707)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:452)
07-07 16:01:38.757: E/AndroidRuntime(3595): at sjtech.rompa.wifi.Bluetoothsetup.onDestroy(Bluetoothsetup.java:233)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.Activity.performDestroy(Activity.java:5495)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1127)
07-07 16:01:38.757: E/AndroidRuntime(3595): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3573)
07-07 16:01:38.757: E/AndroidRuntime(3595): ... 11 more
07-07 16:01:39.976: I/Process(3595): Sending signal. PID: 3595 SIG: 9
对不起,如果格式不正确,这是我的第一次。
这是我认为错误发生的第一页的.java:
package sjtech.rompa.wifi;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import com.javacodegeeks.android.bluetoothtest.R;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class Bluetoothsetup extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
//private static final String TAG = null;
private Button onBtn;
private Button offBtn;
private Button listBtn;
private Button findBtn;
public Button wifiscreen;
private TextView text;
public BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
private ArrayAdapter<String> BTArrayAdapter;
private ProgressBar progressBar1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetoothsetup);
// take an instance of BluetoothAdapter - Bluetooth radio
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//if the device doesnt have bluetooth disable all buttons
if(myBluetoothAdapter == null) {
onBtn.setEnabled(false);
offBtn.setEnabled(false);
listBtn.setEnabled(false);
findBtn.setEnabled(false);
//set 'status' to show:
text.setText("Status: not supported");
//show toast with comment:
Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
Toast.LENGTH_LONG).show();
//if device has bluetooth set on click listener's for buttons
} else {
text = (TextView) findViewById(R.id.text);
onBtn = (Button)findViewById(R.id.turnOn);
onBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
on(v);
}
});
offBtn = (Button)findViewById(R.id.turnOff);
offBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
off(v);
}
});
listBtn = (Button)findViewById(R.id.paired);
listBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list(v);
}
});
findBtn = (Button)findViewById(R.id.search);
findBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
find(v);
}
});
wifiscreen = (Button)findViewById(R.id.wifiscreen);
wifiscreen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Button wifiscreen changes from bluetooth to wifi page
Intent intent = new Intent(v.getContext(), Wifi.class);
startActivity(intent);
}
});
myListView = (ListView)findViewById(R.id.listView1);
// setForeground( Color.RED );
// create the arrayAdapter that contains the BTDevices, and set it to the ListView
BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
myListView.setAdapter(BTArrayAdapter);
}
}
//if bluetooth is turned on request android bluetooth start request
public void on(View view){
if (!myBluetoothAdapter.isEnabled()) {
Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);
//show message for bluetooth start
Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,
Toast.LENGTH_LONG).show();
//show message for bluetooth already on
}
else{
Toast.makeText(getApplicationContext(),"Bluetooth is already on",
Toast.LENGTH_LONG).show();
}
}
// set 'status' to bluetooth status
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == REQUEST_ENABLE_BT){
if(myBluetoothAdapter.isEnabled()) {
text.setText("Status: Enabled");
} else {
text.setText("Status: Disabled");
}
}
}
public void list(View view){
// get paired devices
pairedDevices = myBluetoothAdapter.getBondedDevices();
// put it's one to the adapter
for(BluetoothDevice device : pairedDevices)
BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
// display toast message
Toast.makeText(getApplicationContext(),"Show Paired Devices",
Toast.LENGTH_SHORT).show();
}
final BroadcastReceiver bReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// add the name and the MAC address of the object to the arrayAdapter
BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
BTArrayAdapter.notifyDataSetChanged();
}
}
};
public void find(View view) {
if (myBluetoothAdapter.isDiscovering()) {
//if the button is pressed when it discovers, cancel the discovery
myBluetoothAdapter.cancelDiscovery();
}
else {
BTArrayAdapter.clear();
myBluetoothAdapter.startDiscovery();
registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
}
// show 'status' that bluetooth is disabled
public void off(View view){
myBluetoothAdapter.disable();
text.setText("Status: Disconnected");
//show toast that bluetooth is disabled
Toast.makeText(getApplicationContext(),"Bluetooth turned off",
Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(bReceiver);
}
}
我是否正确地认为它与unregisterReciever相关或在启动第二个活动时关闭应用程序的第一页?我注意到,当更改为第二个活动是应用程序关闭的点,但它似乎关闭,然后恢复到第一个活动而不是完全关闭。
正如我之前提到的,这个确切的代码工作正常,然后这个错误从无处出现,我只是添加了一个按钮另一个活动,我删除了只是在offchance但无济于事。
如果您需要其他代码来帮助我,我很乐意提供它!
提前致谢。
答案 0 :(得分:0)
尝试在onResume()中注册您的接收器,是否可以在onCreate()中注册?
希望它可以帮到你!!
答案 1 :(得分:0)
例如,你在onCreate()中有这样的东西吗?
private void setUpReciever(){
reciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//your code
}
IntentFilter intFilt = new IntentFilter("yourKey");
registerReceiver(reciever, intFilt);
}