2016-10-31 24 views
0

Ich bin eine Android-App codieren. Die App beginnt auf dem Hauptbildschirm und klickt dann auf die Schaltfläche, um die Aktivität zu starten. Mein Code hat keinen Fehler und ist in Ordnung. Das Starten der App und das Klicken auf die Schaltfläche führen jedoch zu einem Absturz.Absturz beim Starten der Aktivität

Mein manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.androidbtcontrol"> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      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=".bluetoothconnect"> 
      <intent-filter> 
       <action android:name="com.example.androidbtcontrol.bluetoothconnect" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

MainActivity.java

public void OnclickButtonListener(){ 
    button_1 = (Button)findViewById(R.id.button); 
    button_1.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent("com.example.androidbtcontrol.bluetoothconnect"); 
        startActivity(intent); 
       } 
      } 
    ); 
     } 
      } 

bluetoothconnect.java

public class bluetoothconnect extends AppCompatActivity { 

    protected void onCreate3(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_bluetoothconnect); 
    } 
    private static final int REQUEST_ENABLE_BT = 1; 

    BluetoothAdapter bluetoothAdapter; 

    ArrayList<BluetoothDevice> pairedDeviceArrayList; 

    TextView textInfo, textStatus; 
    ListView listViewPairedDevice; 
    RelativeLayout inputPane; 

    SeekBar barAnalogOut; 

    ArrayAdapter<BluetoothDevice> pairedDeviceAdapter; 
    private UUID myUUID; 
    private final String UUID_STRING_WELL_KNOWN_SPP = 
      "00001101-0000-1000-8000-00805F9B34FB"; 

    ThreadConnectBTdevice myThreadConnectBTdevice; 
    ThreadConnected myThreadConnected; 


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

     textInfo = (TextView)findViewById(R.id.info); 
     textStatus = (TextView)findViewById(R.id.status); 
     listViewPairedDevice = (ListView)findViewById(R.id.pairedlist); 

     inputPane = (RelativeLayout) findViewById(R.id.inputpane); 

     if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)){ 
      Toast.makeText(this, 
        "FEATURE_BLUETOOTH NOT support", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 

     //using the well-known SPP UUID 
     myUUID = UUID.fromString(UUID_STRING_WELL_KNOWN_SPP); 

     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (bluetoothAdapter == null) { 
      Toast.makeText(this, 
        "Bluetooth is not supported on this hardware platform", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
      return; 
     } 

     String stInfo = bluetoothAdapter.getName() + "\n" + 
       bluetoothAdapter.getAddress(); 
     textInfo.setText(stInfo); 

     barAnalogOut = (SeekBar)findViewById(R.id.analogOut); 
     barAnalogOut.setOnSeekBarChangeListener(OnAnalogOutChangeListener); 

    } 

    SeekBar.OnSeekBarChangeListener OnAnalogOutChangeListener = 
      new SeekBar.OnSeekBarChangeListener(){ 
       @Override 
       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
        //will generate too much data sent! 
        //SendAnalogOut(progress); 
       } 

       @Override 
       public void onStartTrackingTouch(SeekBar seekBar) { 

       } 

       @Override 
       public void onStopTrackingTouch(SeekBar seekBar) { 
        SendAnalogOut(seekBar.getProgress()); 
       } 
      }; 

    private final byte SYNC_BYTE = (byte) 0xAA; 
    private final byte LENGTH_ANALOG = (byte) 3; 
    private final byte CMD_ANALOG = (byte) 1; 

    private void SendAnalogOut(int val){ 
     byte[] bytesToSend = {SYNC_BYTE, LENGTH_ANALOG, CMD_ANALOG, (byte) val}; 
     myThreadConnected.write(bytesToSend); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     //Turn ON BlueTooth if it is OFF 
     if (!bluetoothAdapter.isEnabled()) { 
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
     } 

     setup(); 
    } 

    private void setup() { 
     Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); 
     if (pairedDevices.size() > 0) { 
      pairedDeviceArrayList = new ArrayList<BluetoothDevice>(); 

      for (BluetoothDevice device : pairedDevices) { 
       pairedDeviceArrayList.add(device); 
      } 

      pairedDeviceAdapter = new ArrayAdapter<BluetoothDevice>(this, 
        android.R.layout.simple_list_item_1, pairedDeviceArrayList); 
      listViewPairedDevice.setAdapter(pairedDeviceAdapter); 

      listViewPairedDevice.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        BluetoothDevice device = 
          (BluetoothDevice) parent.getItemAtPosition(position); 
        Toast.makeText(bluetoothconnect.this, 
          "Name: " + device.getName() + "\n" 
            + "Address: " + device.getAddress() + "\n" 
            + "BondState: " + device.getBondState() + "\n" 
            + "BluetoothClass: " + device.getBluetoothClass() + "\n" 
            + "Class: " + device.getClass(), 
          Toast.LENGTH_LONG).show(); 

        textStatus.setText("start ThreadConnectBTdevice"); 
        myThreadConnectBTdevice = new ThreadConnectBTdevice(device); 
        myThreadConnectBTdevice.start(); 
       } 
      }); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

     if(myThreadConnectBTdevice!=null){ 
      myThreadConnectBTdevice.cancel(); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode==REQUEST_ENABLE_BT){ 
      if(resultCode == Activity.RESULT_OK){ 
       setup(); 
      }else{ 
       Toast.makeText(this, 
         "BlueTooth NOT enabled", 
         Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } 
    } 

    //Called in ThreadConnectBTdevice once connect successed 
    //to start ThreadConnected 
    private void startThreadConnected(BluetoothSocket socket){ 

     myThreadConnected = new ThreadConnected(socket); 
     myThreadConnected.start(); 
    } 

    /* 
    ThreadConnectBTdevice: 
    Background Thread to handle BlueTooth connecting 
    */ 
    private class ThreadConnectBTdevice extends Thread { 

     private BluetoothSocket bluetoothSocket = null; 
     private final BluetoothDevice bluetoothDevice; 


     private ThreadConnectBTdevice(BluetoothDevice device) { 
      bluetoothDevice = device; 

      try { 
       bluetoothSocket = device.createRfcommSocketToServiceRecord(myUUID); 
       textStatus.setText("bluetoothSocket: \n" + bluetoothSocket); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void run() { 
      ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton); 
      toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if (isChecked) { 
         SendAnalogOut(0); 
         // The toggle is enabled 
        } else { 
         SendAnalogOut(255); 
         // The toggle is disabled 
        } 
       } 
      }); 
      boolean success = false; 
      try { 
       bluetoothSocket.connect(); 
       success = true; 
      } catch (IOException e) { 
       e.printStackTrace(); 

       final String eMessage = e.getMessage(); 
       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         textStatus.setText("Something wrong BluetoothSocket.connect(): \n" + eMessage); 
        } 
       }); 

       try { 
        bluetoothSocket.close(); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
      } 

      if(success){ 
       //connect successful 
       final String msgconnected = "Connect Successful:\n" 
         + "BluetoothSocket: " + bluetoothSocket + "\n" 
         + "BluetoothDevice: " + bluetoothDevice; 

       runOnUiThread(new Runnable(){ 

        @Override 
        public void run() { 
         textStatus.setText(msgconnected); 

         listViewPairedDevice.setVisibility(View.GONE); 
         inputPane.setVisibility(View.VISIBLE); 
        }}); 

       startThreadConnected(bluetoothSocket); 
      }else{ 
       //fail 
      } 
     } 

     public void cancel() { 

      Toast.makeText(getApplicationContext(), 
        "close bluetoothSocket", 
        Toast.LENGTH_LONG).show(); 

      try { 
       bluetoothSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
     }  
    } 

    /* 
    ThreadConnected: 
    Background Thread to handle Bluetooth data communication 
    after connected 
    */ 
    private class ThreadConnected extends Thread { 
     private final BluetoothSocket connectedBluetoothSocket; 
     private final InputStream connectedInputStream; 
     private final OutputStream connectedOutputStream; 

     public ThreadConnected(BluetoothSocket socket) { 
      connectedBluetoothSocket = socket; 
      InputStream in = null; 
      OutputStream out = null; 

      try { 
       in = socket.getInputStream(); 
       out = socket.getOutputStream(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      connectedInputStream = in; 
      connectedOutputStream = out; 
     } 

     @Override 
     public void run() { 
      byte[] buffer = new byte[1024]; 
      int bytes; 

      while (true) { 
       try { 
        bytes = connectedInputStream.read(buffer); 
        String strReceived = new String(buffer, 0, bytes); 
        final String msgReceived = String.valueOf(bytes) + 
          " bytes received:\n" 
          + strReceived; 

        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          textStatus.setText(msgReceived); 

         }}); 

       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 

        final String msgConnectionLost = "Connection lost:\n" 
          + e.getMessage(); 
        runOnUiThread(new Runnable(){ 

         @Override 
         public void run() { 
          textStatus.setText(msgConnectionLost); 
         }}); 
       } 
      } 
     } 

     public void write(byte[] buffer) { 
      try { 
       connectedOutputStream.write(buffer); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     public void cancel() { 
      try { 
       connectedBluetoothSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

Haupt Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:autoLink="web" 
     android:text="Please Choose " 
     android:textStyle="bold" /> 

    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" /> 
</LinearLayout> 

Aktivität Layout-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_bluetoothconnect" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.androidbtcontrol.bluetoothconnect"> 
    <TextView 
     android:id="@+id/info" 
     android:textStyle="bold|italic" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/status" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <ListView 
     android:id="@+id/pairedlist" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <RelativeLayout 
     android:id="@+id/inputpane" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:visibility="gone"> 
    </RelativeLayout> 

    <SeekBar 
     android:id="@+id/analogOut" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:max="99" 
     android:progress="0"/> 

    <ToggleButton 
     android:text="ToggleButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/toggleButton" />  

</RelativeLayout> 

Antwort

0

Dies sind einige der Fehler, die ich aus den Informationen, die Sie gegeben haben, erkennen. Veröffentlichen Sie die Logcat-Ausnahmedetails, damit ich diese Lösung verfeinern kann.

1) Verwenden Sie nicht Ihre eigene Version von onCreate (in bluetoothactivity). Eine Aktivität sollte nur eine onCreate Methode haben. Außerdem müssen Sie der onCreate-Methode das Präfix @Override hinzufügen. Der Code, den Sie haben, enthält onCreate2 und onCreate3 Methoden, die fehlerhaft sind. Das könnte der Grund für den Absturz sein, denn wenn die Bluetooth-Aktivität aufgerufen wird, kann sie die onCreate-Methode nicht finden.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Keep your bluetooth related functionality here. 


} 

2) Wenn Ihre Absicht starten, müssen Sie auch die Quelle und das Ziel angeben.

Intent intent = new Intent(CallingActivity.this,CalledActivity.class); 
startActivity(intent); 
+0

ich den Absturzfehler behoben angeben müssen was hast du gesagt. Ich habe auch das falsche Layout in main aufgerufen. Nicht in der Bluetooth-Funktionalität. –

+0

Gerne helfen :) – SoulRayder

0

Ihr Absturz vielleicht aufgrund Ihrer Absicht verursacht, in Tätigkeit

Intent intent = new Intent("com.example.androidbtcontrol.bluetoothconnect"); 
       startActivity(intent); 

der richtige Weg

Intent intent=new Intent(firstactivity.this,secondactivity.class); 
startactivity(intent); 

Sie, indem Sie das Ziel

Verwandte Themen