我不知道为什么我的程序在运行时会停止。该应用程序什么也不显示,我只收到错误消息:“该应用程序已关闭”,但是屏幕上什么都没有显示,背景和应用程序中的按钮都没有出现。
我注意到将图片附加到按钮上时会发生这种情况
公共类Telecomanda扩展了AppCompatActivity {
public Button btnFata, btnFataDreapta, btnFataStanga, btnSpate, btnFar, btnGaraj, btnObstacol;
public BluetoothAdapter btAdapter;
public BluetoothDevice device;
public BluetoothSocket socket;
public OutputStream outputStream;
String ip = "192.168.1.102";
String comand;
public final UUID PORT_UUID = UUID.fromString( "00001101-0000-1000-8000-00805f9b34fb" ); //SPP UUID
public final String DEVICE_ADDRESS = "00:18:E4:35:08:B6"; // HC-05 BT ADDRESS
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_telecomanda );
this.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
outputStream = new ByteArrayOutputStream( 1024 );
// declarare butoane
btnFata = (Button) findViewById( R.id.btnFata );
btnSpate = (Button) findViewById( R.id.btnSpate );
btnFataDreapta = (Button) findViewById( R.id.btnFataDreapta );
btnFataStanga = (Button) findViewById( R.id.btnFataStanga );
btnGaraj = (Button) findViewById( R.id.btnGaraj );
btAdapter = BluetoothAdapter.getDefaultAdapter();
device = btAdapter.getRemoteDevice( DEVICE_ADDRESS );
if (btAdapter == null) {
Toast.makeText( getApplicationContext(), "Bluetooth not available", Toast.LENGTH_LONG ).show();
} else {
if (!btAdapter.isEnabled()) {
Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
startActivityForResult( enableIntent, 3 );
} else {
ConnectThread connectThread = new ConnectThread( device );
connectThread.start();
}
}
btnFata.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
comand = "1";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
comand = "5";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
} );
btnFataDreapta.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
comand = "3";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
comand = "5";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
} );
btnFataStanga.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
comand = "4";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
comand = "5";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
} );
btnSpate.setOnTouchListener( new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
comand = "2";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
comand = "5";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
} );
btnObstacol.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (socket != null) {
comand = "6";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
} else {
comand = "5";
try {
outputStream.write( comand.getBytes() );
} catch (IOException e) {
e.printStackTrace();
}
}
}
} );
btnGaraj.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
commandArduino( "http://" + ip + "/?cmd=1" );
}
} );
}
public void commandArduino(String url) {
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute( new HttpGet( url ) );
} catch (Exception e) {
}
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/images"
android:minHeight="0dp"
android:visibility="visible"
tools:context="com.td.daniela.control_masina.Telecomanda">
<Button
android:id="@+id/btnFata"
style="@style/Widget.AppCompat.Button"
android:layout_width="98dp"
android:layout_height="65dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/fata"
android:elevation="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.096" />
<Button
android:id="@+id/btnSpate"
style="@style/Widget.AppCompat.Button"
android:layout_width="98dp"
android:layout_height="65dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/spate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.9" />
<Button
android:id="@+id/btnFataDreapta"
style="@style/Widget.AppCompat.Button"
android:layout_width="98dp"
android:layout_height="65dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="@drawable/stanga"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.306"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnFataStanga"
style="@style/Widget.AppCompat.Button"
android:layout_width="98dp"
android:layout_height="65dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@mipmap/dreapta"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.693"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnGaraj"
android:layout_width="98dp"
android:layout_height="65dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />