为什么它会告诉我"不幸的是,应用程序停止了#34;

时间:2015-07-04 08:44:43

标签: android

大家好,我开始原谅我因为语法错误的句子,我的母语不是英语。

我写了一个代码,用于打开和...蓝牙 我在最后放了一个菜单,只有一个项目出口 但当我点击它显示我"不幸的是,应用已停止" 请帮帮我

MainActivity如下所示:

package com.example.application;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import java.util.Set;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;



public class MainActivity extends Activity {

private static final int REQUEST_ENABLE_BT = 1;

private Button listBtn;
private Button findBtn;
private TextView text;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
private ArrayAdapter<String> BTArrayAdapter;


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

  myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if(myBluetoothAdapter == null) {

      listBtn.setEnabled(false);
      findBtn.setEnabled(false);
      text.setText("Status: not supported");

      Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth!",
             Toast.LENGTH_LONG).show();
   } 





    //---------------------------------------------------------
     else {

       final ImageView iv = (ImageView) findViewById(R.id.imageView1);
       final ToggleButton tb = (ToggleButton)  findViewById(R.id.toggleButton1);
      text = (TextView) findViewById(R.id.text);



       iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(!(tb.isChecked())){
                 if (!myBluetoothAdapter.isEnabled()) {
                     Intent turnOnIntent = new  Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                     startActivityForResult(turnOnIntent,  REQUEST_ENABLE_BT);

                      Toast.makeText(getApplicationContext(),"Bluetooth  turned on !" ,
                             Toast.LENGTH_LONG).show();
                     tb.toggle();
                     iv.setImageResource(R.drawable.on);

                  }
                  else{
                     Toast.makeText(getApplicationContext(),"Bluetooth is already on !",
                             Toast.LENGTH_LONG).show();
                  }
            }else{

                 myBluetoothAdapter.disable();
                  text.setText("Status: Disconnected");

                  Toast.makeText(getApplicationContext(),"Bluetooth turned off !",
                          Toast.LENGTH_LONG).show();
                  tb.toggle();
                  iv.setImageResource(R.drawable.off);

            }
        }
      });



      listBtn = (Button)findViewById(R.id.paired);
      listBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            list(v);
        }
      });

      findBtn = (Button)findViewById(R.id.search);
      findBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            find(v);
        }
       });

        myListView = (ListView)findViewById(R.id.listView1);

        // create the arrayAdapter that contains the BTDevices, and set it to  the ListView
        BTArrayAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1);
       myListView.setAdapter(BTArrayAdapter);
    }
 }


    //---------------------------------------------------------



   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent  data)    {
     // TODO Auto-generated method stub
     if(requestCode == REQUEST_ENABLE_BT){
       if(myBluetoothAdapter.isEnabled()) {
           text.setText("Status: Enabled");
       } else {   
           text.setText("Status: Disabled");
       }
      }
    }

   public void list(View view){
   // get paired devices
   pairedDevices = myBluetoothAdapter.getBondedDevices();

   // put it's one to the adapter
   for(BluetoothDevice device : pairedDevices)
      BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());

   Toast.makeText(getApplicationContext(),"Show Paired Devices",
          Toast.LENGTH_SHORT).show();

}

    final BroadcastReceiver bReceiver = new BroadcastReceiver() {
     public void onReceive(Context context, Intent intent) {
         String action = intent.getAction();
         // When discovery finds a device
         if (BluetoothDevice.ACTION_FOUND.equals(action)) {
             // Get the BluetoothDevice object from the Intent
             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             // add the name and the MAC address of the object to the arrayAdapter
             BTArrayAdapter.add(device.getName() + "\n" +   device.getAddress());
             BTArrayAdapter.notifyDataSetChanged();
         }
     }
    };

    public void find(View view) {
     if (myBluetoothAdapter.isDiscovering()) {
       // the button is pressed when it discovers, so cancel the discovery
       myBluetoothAdapter.cancelDiscovery();
     }
      else {
        BTArrayAdapter.clear();
        myBluetoothAdapter.startDiscovery();

        registerReceiver(bReceiver, new      IntentFilter(BluetoothDevice.ACTION_FOUND));   
        }    
    }



   @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
   super.onDestroy();
    unregisterReceiver(bReceiver);
  }


  // menu is shown here
  //---------------------------------------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.mainmenu,menu );
    return true;
    }
   //-------------------------------------------------- 
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.item1)
    {

        this.finish();

    }
    return super.onOptionsItemSelected(item);
    }
    //-------------------------------------------------

   }

我应该在菜单代码中做什么...

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
 MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu,menu );
return true;
}
//-------------------------------------------------- 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.item1)
{

======>>>>>>>>>>    this.finish();       <<<<<<===========

}
return super.onOptionsItemSelected(item);
}
//-------------------------------------------------

0 个答案:

没有答案