android错误:ViewRootImpl $ CalledFromWrongThreadException

时间:2015-12-30 06:15:44

标签: java android multithreading

我知道之前有人问过不同的程序,但我仍然会收到以下错误的程序

线程[Thread-15956](暂停(异常ViewRootImpl $ CalledFromWrongThreadException))

请帮助!!

这是我的MainActivity类

package a.example.na;

import java.util.Scanner;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    private static TextView inputtext;
    private BluetoothAdapter myBluetoothAdapter;
       private Set<BluetoothDevice> pairedDevices;
       private ListView myListView;
       private ArrayAdapter<String> BTArrayAdapter;
       private BluetoothDevice mDevice;
       private Button findBtn;
       private String address;


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


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



              findBtn.setEnabled(false);


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






            inputtext = (TextView) findViewById(R.id.text); 
        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);
          myListView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> adapterView, View v, int pos,
                    long arg3) {

                        // Add the name and address to an array adapter to show in a ListView
                        String i = (String) adapterView.getItemAtPosition(pos);
                         B b=new B(inputtext);
                            b.start();
                        Scanner scanner = new Scanner(i);

                        if(scanner.hasNextLine()) {

                            address =scanner.nextLine();
                        }




                        mDevice = myBluetoothAdapter.getRemoteDevice(address);

                            scanner.close();










              }
          });


          }
    }
     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.getAddress()+ "\n" +device.getName() );

                     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
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

这是我单独的Thread类

package com.bt;

import android.widget.TextView;

public class B extends Thread {
 TextView inputtext;
    public B(TextView x){
        inputtext=x;
    }

    public void run(){


        inputtext.setText("hero");  
    }
}

1 个答案:

答案 0 :(得分:-1)

您正在触摸非UI线程中的视图(小部件)。

public class B extends Thread {
    TextView inputtext;
    Activity activity;

    public B(Activity activity, TextView x) {
        inputtext = x;
        this.activity = activity;
    }

    public void run() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                inputtext.setText("hero");
            }
        });
    }
}

启动线程时

B b=new B(MainActivity.this, inputtext); 
b.start();