为什么我在Android中收到“未被捕获的异常”的“线程”?

时间:2015-05-20 08:43:16

标签: android

我尝试实施的内容是我的OnClick ListView,所以当我点击ListView中的tab1时,我想将标签切换为{ {1}}然后我想保存点击的tab2行,以便在ListView的同一ListView中显示但有更多信息。但是我收到了这个错误。

为什么我收到此错误,如何在tab2tab1中显示所选行?

tab2

错误消息

public class Tab1 extends TabActivity {

    WifiManager wifiManager;
    WifiScanReceiver wifiReciever;
    String ssid;
    int channel;
    int level;
    ArrayAdapter adapter;
    ListView list;
    ArrayList<String> wifis;
    WifiInfo wifiInfo;
    TabHost tabHost = null;

    // TabSpec Names
    private static final String TAB1 = "Tab1";
    private static final String TAB2 = "Tab2";
    private static final String TAB3 = "Tab3";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab1);

        list = (ListView) findViewById(R.id.text);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        wifiInfo = wifiManager.getConnectionInfo();

        wifis = new ArrayList<String>(); //initialize wifis
        wifis.add("loading...");
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, wifis);
        list.setAdapter(adapter);

        wifiManager.startScan(); //make sure this is the last call
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                v.findViewById(R.id.text);
                switchTab(1);
            }
        }); };

    public void switchTabs(int tab) {
        tabHost.setCurrentTab(tab);
    }

    public void switchTab(int tab) {
        tabHost.setCurrentTab(tab);
    }

    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    public static int convertFrequencyToChannel(int freq) {
        if (freq >= 2412 && freq <= 2484) {
            return (freq - 2412) / 5 + 1;
        } else if (freq >= 5170 && freq <= 5825) {
            return (freq - 5170) / 5 + 34;
        } else {
            return -1;
        }
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }


    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            wifis.clear(); //add this
            for (int i = 0; i < wifiScanList.size(); i++) {
                ssid = wifiScanList.get(i).SSID; //Get the SSID
                level = wifiScanList.get(i).level;
                channel = wifiScanList.get(i).frequency;

                //use add here:
                wifis.add(ssid + " " + level + " dBm "); //append to the other data
                wifis.toString();
            }

            adapter.notifyDataSetChanged(); //add this
            wifiManager.startScan(); //start a new scan to update values faster

        }
    }

}

2 个答案:

答案 0 :(得分:1)

您没有在整个代码中初始化tabHost对象,因为此对象未初始化您的代码:

 public void switchTab(int tab) {
        tabHost.setCurrentTab(tab); // Throwing exception here
    }

tabHost = null此处。先设置它。

答案 1 :(得分:0)

请看看这两个教程:

http://developer.android.com/reference/android/app/TabActivity.html

http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html

如果您不知道,只需添加一些关于基本调试的内容。

将来,您可能会遇到超过一千行代码,并且发现异常的原因变得越来越困难。因此,学习如何调试非常重要。

可在此处找到一些调试介绍:

https://developer.android.com/tools/debugging/debugging-studio.html

详细解释: https://www.jetbrains.com/idea/help/debugging.html

一些简要说明:

  1. 为问题/错误或任何部分添加断点

  2. F5进入调试模式

  3. 检查值 (对于你的情况:nullpointerexception你可以检查哪个值为空,这大部分是tabhost,因为你没有初始化它)

  4. 您也可以使用threads选项卡来检查将来的多线程问题。