gpt4 book ai didi

Android 系统服务错误,在 onCreate() 之前服务不可用

转载 作者:行者123 更新时间:2023-11-29 21:13:00 25 4
gpt4 key购买 nike

我正在尝试在 android(目标 4.3)中使用带有操作栏的 fragment 概念。为此,我使用了有效的导航应用程序(作为我的基础和构建)。我喜欢滑动 View 的想法,并希望保留此功能。

Link to swipe view android page

我在实现应用程序和使用 fragment 的基本功能方面没有遇到任何问题,但事情变得棘手了。我之前开发了一个可以工作的蓝牙应用程序(它本身可以工作),现在我正试图将它作为我的新应用程序中的一个 fragment 来实现。目前,我的应用程序中的操作栏具有三个选项卡:欢迎页面、传感器和 GPS。对于这个问题,我只关心传感器选项卡。

安卓代码

我的代码超过 700 行,所以我只会在这里发布相关的 spinets。如果您需要其他任何东西,请发表评论,我会提供。

    /* Relevant imports here 
* including: android.support.v7.app.ActionBar;
* android.support.v7.app.ActionBarActivity;
* android.support.v4.app.FragmentTransaction;
*/

主 Activity 类

 AppSectionsPagerAdapter mAppSectionsPagerAdapter;

/**
* The {@link ViewPager} that will display the three primary sections of the app, one at a
* time.
*/
ViewPager mViewPager;

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

// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();

// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(false);

// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});


// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i){
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LaunchpadSectionFragment();

case 1:
return new BluetoothClass();

default:
// The GPS section of the app .
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}

@Override
public int getCount() {
return 3;
}

@Override
public String getPageTitle(int position) {
if (position == 0 ){
return "Welcome Page";
}
if (position == 1 ){
return "Sensors";
}

return "GPS";
}
}

除此之外

蓝牙类

在 MainActivity 中找到

public static class BluetoothClass extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
/*
* Bluetooth Class Variable definitions
*/
Button button;
ToggleButton toggle_discovery;
ListView listView;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ArrayAdapter<String> mArrayAdapter;
ArrayAdapter<String> adapter;
ArrayList<String> pairedDevicesList;
ArrayList<String> unpairedDevicesList;
ArrayList<String> combinedDevicesList;
Set<BluetoothDevice> pairedDevices;
Set<String> unpairedDevices;
BroadcastReceiver mReceiver;
String selectedFromList;
String selectedFromListName;
String selectedFromListAddress;
BluetoothDevice selectedDevice;
ActionBarActivity aba = new ActionBarActivity();

/*
* Important Bluetooth specific variables
* and other important data variables
*
*/
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
final int STATE_CONNECTED = 2;
final int STATE_CONNECTING = 1;
final int STATE_DISCONNECTED = 0;
private final UUID MY_UUID = UUID.fromString("0001101-0000-1000-8000-00805F9B34FB");
private static final int REQUEST_ENABLE_BT = 1;
public byte[] completeData;


/*
* Bluetooth Handler Method
*/
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// Do Something;
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_LONG).show();
//String s = "This string proves a socket connection has been established!!";
String s = "test";
connectedThread.write(s.getBytes());
connectedThread.start();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
if (string.contains("!")){
//Do nothing!!
}else{
Toast.makeText(getActivity(),string,Toast.LENGTH_SHORT).show();
}


break;
}
}
};

public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
enableBT();
pairedDevicesList = new ArrayList<String>();
unpairedDevicesList = new ArrayList<String>();
unpairedDevices = new HashSet<String>();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.bluetooth, container,false);
Toast.makeText(getActivity(), "Fragment Created",Toast.LENGTH_SHORT).show();
rootview.findViewById(R.id.findDevices).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enableBT();
mArrayAdapter = new ArrayAdapter<String>(aba,android.R.layout.simple_list_item_1,removeDuplicates(unpairedDevicesList,pairedDevicesList));
pairedDevices = mBluetoothAdapter.getBondedDevices();
displayCominedDevices(mArrayAdapter);
}
});

return rootview;
}

public void onActivityCreared(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
}



public void onStart(){
super.onStart();
Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
}

public void onResume(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();

}

public void onStop(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
disableBT();
}


public void enableBT(){
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(getActivity(), "Bluetooth is not suppourted on Device",Toast.LENGTH_SHORT).show();
}

if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
int resultCode = Activity.RESULT_OK;
if(resultCode < 1){
Toast.makeText(getActivity(), "Please Accept Enabling Bluetooth Request!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getActivity(), "Enabling Bluetooth FAILED!", Toast.LENGTH_SHORT).show();
}
}
}

public void disableBT(){
if (mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
}
}

/*
* Display Helper Methods
*/
public void displayCominedDevices(ArrayAdapter<String> mArrayAdapter){
displayPairedDevices();
displayDetectedDevices();
ActionBarActivity aba = new ActionBarActivity();
listView = (ListView) getView().findViewById(R.id.listView);
//mArrayAdapter = new ArrayAdapter<String>(aba,android.R.layout.simple_list_item_1,removeDuplicates(unpairedDevicesList,pairedDevicesList));
listView.setAdapter(mArrayAdapter);
}


public void displayPairedDevices(){
// If there are paired devices
enableBT();
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
String s = " ";
String deviceName = device.getName();
String deviceAddress = device.getAddress();
pairedDevicesList.add(deviceName + s + deviceAddress +" \n");
}
}
}

public void displayDetectedDevices(){
mBluetoothAdapter.startDiscovery();

// Create a BroadcastReceiver for ACTION_FOUND
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceAddress = device.getAddress();
String s = " ";
unpairedDevices.add(deviceName + s + deviceAddress +" \n");
unpairedDevicesList = new ArrayList<String>(unpairedDevices);
}
}
};
}

public ArrayList<String> removeDuplicates(ArrayList<String> s1, ArrayList<String> s2){
combinedDevicesList = new ArrayList<String>();
combinedDevicesList.addAll(s1);
combinedDevicesList.addAll(s2);
Set Unique_set = new HashSet(combinedDevicesList);
combinedDevicesList = new ArrayList<String>(Unique_set);
/*Debugging
Toast.makeText(getApplication(),"Combined List" + combinedDevicesList.toString(),Toast.LENGTH_LONG).show(); */
return combinedDevicesList;
}


/*
* Bluetooth Connection Threads
*/

public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {

/*
* Use a temporary object that is later assigned to mmSocket,
* because mmSocket is final
*/

BluetoothSocket tmp = null;

mmDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}

public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();

try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
Toast.makeText(getActivity(), "Connecting to device failed!", Toast.LENGTH_LONG).show();
}
return;
}

// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}

}

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()

// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[9800];
bytes = mmInStream.read(buffer,0,buffer.length);

// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}

/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);


} catch (IOException e) { }
}
}

}

问题

所以一切正常,直到我单击位于我的应用程序上的蓝牙类 (bluetooth.xml) 文件中的 findDevices 按钮。

  1. 根据 Logcat Logcat当我尝试声明 mArrayAdapter 时出现问题。我不明白为什么我会收到错误,因为我在 fragment 的 onCreateView() 部分(在 onCreate() 部分之后)实现了 mArrayApapter 声明,所以 logCat 的错误没有意义。

Link to fragments android page

最佳答案

这里:

ActionBarActivity aba = new ActionBarActivity();

您正在尝试实例化一个 Activity!

不要那样做。在 Android 中启动 Activity 的方法是使用 startActivity 方法!

所以你实际上做了两次。但是你的 logcat 提示的似乎是第二个:你在 onClick 中调用这个: > 你不应该做的事情。

关于Android 系统服务错误,在 onCreate() 之前服务不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com