我有一个带有某些选项的导航抽屉(家庭,蓝牙,警报和提示)。我将活动链接到“蓝牙”和“警报”,但是“家用”出现了一些问题。 运行该应用程序时,我有一个连接到Home.java的启动屏幕,在此活动中,我可以单击我的抽屉并选择其他的alarm.java anche bluetooth.java,但是当我将活动链接到家庭时,使用同样的方法,我的应用程序停止工作。 非常感谢!
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.progetto.app">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
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=".Home"
android:label="@string/Home"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Alarm"
android:label="@string/title_activity_alarm"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Bluetooth"
android:label="@string/title_activity_bt"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Tips"
android:label="@string/title_activity_tips"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".AlarmActivity" />
<receiver android:name=".Alarm_Receiver" />
<activity android:name=".DeviceListActivity" />
<activity android:name=".ArduinoMain"/>
<service
android:name=".RingtonePlayingService"
android:enabled="true" />
</application>
</manifest>
这是我的带抽屉导航的Home.java
package com.progetto.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.nav_home:
Intent h = new Intent(Home.this,ArduinoMain.class);
startActivity(h);
break;
case R.id.nav_bt:
Intent i = new Intent(Home.this,DeviceListActivity.class);
startActivity(i);
break;
case R.id.nav_alarm:
Intent g = new Intent(Home.this,AlarmActivity.class);
startActivity(g);
break;
case R.id.nav_tips:
Intent s = new Intent(Home.this,Tips.class);
startActivity(s);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是我要连接到家庭的活动,我希望在闲聊屏幕结束时看到此活动
package com.progetto.app;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ArduinoMain extends Activity {
//Declare buttons & editText
Button functionOne, functionTwo;
private EditText editText;
//Memeber Fields
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
// UUID service - This is the type of Bluetooth device that the BT module is
// It is very likely yours will be the same, if not google UUID for your manufacturer
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// MAC-address of Bluetooth module
public String newAddress = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arduino_main);
addKeyListener();
//Initialising buttons in the view
//mDetect = (Button) findViewById(R.id.mDetect);
functionOne = (Button) findViewById(R.id.functionOne);
functionTwo = (Button) findViewById(R.id.functionTwo);
//getting the bluetooth adapter value and calling checkBTstate function
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
/**************************************************************************************************************************8
* Buttons are set up with onclick listeners so when pressed a method is called
* In this case send data is called with a value and a toast is made
* to give visual feedback of the selection made
*/
functionOne.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("1");
Toast.makeText(getBaseContext(), "Function 1", Toast.LENGTH_SHORT).show();
}
});
functionTwo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("2");
Toast.makeText(getBaseContext(), "Function 2", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResume() {
super.onResume();
// connection methods are best here in case program goes into the background etc
//Get MAC address from DeviceListActivity
Intent intent = getIntent();
newAddress = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Set up a pointer to the remote device using its address.
BluetoothDevice device = btAdapter.getRemoteDevice(newAddress);
//Attempt to create a bluetooth socket for comms
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
Toast.makeText(getBaseContext(), "ERROR - Could not create Bluetooth socket", Toast.LENGTH_SHORT).show();
}
// Establish the connection.
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close(); //If IO exception occurs attempt to close socket
} catch (IOException e2) {
Toast.makeText(getBaseContext(), "ERROR - Could not close Bluetooth socket", Toast.LENGTH_SHORT).show();
}
}
// Create a data stream so we can talk to the device
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "ERROR - Could not create bluetooth outstream", Toast.LENGTH_SHORT).show();
}
//When activity is resumed, attempt to send a piece of junk data ('x') so that it will fail if not connected
// i.e don't wait for a user to press button to recognise connection failure
sendData("x");
}
@Override
public void onPause() {
super.onPause();
//Pausing can be the end of an app if the device kills it or the user doesn't open it again
//close all connections so resources are not wasted
//Close BT socket to device
try {
btSocket.close();
} catch (IOException e2) {
Toast.makeText(getBaseContext(), "ERROR - Failed to close Bluetooth socket", Toast.LENGTH_SHORT).show();
}
}
//takes the UUID and creates a comms socket
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
//same as in device list activity
private void checkBTState() {
// Check device has Bluetooth and that it is turned on
if(btAdapter==null) {
Toast.makeText(getBaseContext(), "ERROR - Device does not support bluetooth", Toast.LENGTH_SHORT).show();
finish();
} else {
if (btAdapter.isEnabled()) {
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
// Method to send data
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
try {
//attempt to place data on the outstream to the BT device
outStream.write(msgBuffer);
} catch (IOException e) {
//if the sending fails this is most likely because device is no longer there
Toast.makeText(getBaseContext(), "ERROR - Device not found", Toast.LENGTH_SHORT).show();
finish();
}
}
public void addKeyListener() {
// get edittext component
editText = (EditText) findViewById(R.id.editText1);
// add a keylistener to keep track user input
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// if keydown and send is pressed implement the sendData method
if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
//I have put the * in automatically so it is no longer needed when entering text
sendData('*' + editText.getText().toString());
Toast.makeText(getBaseContext(), "Sending text", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
}
这是启动屏幕
package com.progetto.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class SplashScreen extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
iv=(ImageView) findViewById(R.id.iv);
Animation myanim= AnimationUtils.loadAnimation(this,R.anim.mytransition);
iv.startAnimation(myanim);
final Intent i=new Intent(this, Home.class);
Thread timer =new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
startActivity(i);
finish();
}
}
};
timer.start();
}
}
这是项目 DropBox
这是apk Apk