我试图在android中使用带有动作栏的片段的概念(目标4.3)。为此,我使用了有效的导航应用程序(作为我的基础并构建)。我喜欢滑动视图的想法,并希望保留此功能。
Link to swipe view android page
我在实现应用程序方面没有任何问题,并且具有片段的基本功能,但这里的事情变得棘手。我以前开发了一个工作蓝牙应用程序(它自己工作),我现在正试图在我的新应用程序中将其实现为片段。目前,我的应用中的操作栏有三个标签,一个欢迎页面,传感器和GPS。对于这个问题,我只关心传感器选项卡。
我的代码超过700行,所以我只会在这里发布相关的spinets。评论你是否需要其他任何东西&我会提供它。
/* Relevant imports here
* including: android.support.v7.app.ActionBar;
* android.support.v7.app.ActionBarActivity;
* android.support.v4.app.FragmentTransaction;
*/
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) { }
}
}
}
所以一切正常,直到我点击我的应用程序中的findDevices按钮,在我的蓝牙类(bluetooth.xml)文件中。
答案 0 :(得分:2)
下面:
ActionBarActivity aba = new ActionBarActivity();
你正试图实例化一个活动!
不要那样做。在Android中启动活动的方法是使用startActivity
方法!
所以你实际上这样做了两次。但是你的logcat抱怨的那个似乎是第二个:你在onClick中调用它:displayCominedDevices(mArrayAdapter);
并且在这个方法里面是你不应该做的new ActionBarActivity(),
事情。