我尝试实施的内容是我的OnClick
ListView
,所以当我点击ListView
中的tab1
时,我想将标签切换为{ {1}}然后我想保存点击的tab2
行,以便在ListView
的同一ListView
中显示但有更多信息。但是我收到了这个错误。
为什么我收到此错误,如何在tab2
到tab1
中显示所选行?
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
}
}
}
答案 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
一些简要说明:
为问题/错误或任何部分添加断点
F5进入调试模式
检查值
(对于你的情况:nullpointerexception
你可以检查哪个值为空,这大部分是tabhost
,因为你没有初始化它)
您也可以使用threads选项卡来检查将来的多线程问题。