我有两种方法:
private void openPrivate(String target) {
currentKeyWindows = target;
Log.i("better","openPrivate "+ target);
if (windows.containsKey(currentKeyWindows)) {
} else {
tw = createTextView(currentKeyWindows);
ActionBar.Tab newTab = getSupportActionBar().newTab();
newTab.setText(currentKeyWindows);
newTab.setTabListener(MainActivity.this);
getSupportActionBar().addTab(newTab, true);
}
}
private void jointochannel(String target) {
currentKeyWindows = "#" + target;
Log.i("better","jointochannel "+ target);
if (windows.containsKey(currentKeyWindows)) {
} else {
tw = createTextView(currentKeyWindows);
listofnicks.put(currentKeyWindows, new ArrayList<String>());
nicklistadapter = new ArrayAdapter<String>(this, R.xml.mylistview, listofnicks.get(currentKeyWindows));
nicklist.setAdapter(nicklistadapter);
registerForContextMenu(nicklist);
ActionBar.Tab newTab = getSupportActionBar().newTab();
newTab.setText(currentKeyWindows);
newTab.setTabListener(MainActivity.this);
getSupportActionBar().addTab(newTab, true);
irc.addCommand(new Command(Command.JOIN_TO_CHANNEL, "JOIN " + currentKeyWindows));
}
channel = currentKeyWindows;
}
第二种方法效果很好。第一种方法(openPrivate)以空点异常结束:
getSupportActionBar().addTab(newTab, true);
我的 AndroidManifest.xml:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
该怎么办?为什么相同的代码工作方式不同?问候,丹尼斯。
日志猫:
11-04 17:06:57.241: E/AndroidRuntime(4696): FATAL EXCEPTION: main
11-04 17:06:57.241: E/AndroidRuntime(4696): java.lang.NullPointerException
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.widget.ListView.setAdapter(ListView.java:454)
11-04 17:06:57.241: E/AndroidRuntime(4696): at ru.startandroid.develop.MainActivity.onTabSelected(MainActivity.java:427)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:526)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.actionbarsherlock.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:452)
11-04 17:06:57.241: E/AndroidRuntime(4696): at ru.startandroid.develop.MainActivity.openPrivate(MainActivity.java:382)
11-04 17:06:57.241: E/AndroidRuntime(4696): at ru.startandroid.develop.MainActivity.onContextItemSelected(MainActivity.java:100)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.app.Activity.onMenuItemSelected(Activity.java:2209)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2785)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.widget.ListView.performItemClick(ListView.java:3513)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.os.Handler.handleCallback(Handler.java:587)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.os.Looper.loop(Looper.java:130)
11-04 17:06:57.241: E/AndroidRuntime(4696): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-04 17:06:57.241: E/AndroidRuntime(4696): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 17:06:57.241: E/AndroidRuntime(4696): at java.lang.reflect.Method.invoke(Method.java:507)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-04 17:06:57.241: E/AndroidRuntime(4696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-04 17:06:57.241: E/AndroidRuntime(4696): at dalvik.system.NativeStart.main(Native Method)
onTabSelected(Tab 选项卡,FragmentTransaction ft):
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
currentKeyWindows = tab.getText().toString();
mScrollView.removeAllViews();
mScrollView.addView(this.windows.get(this.currentKeyWindows));
scrollToBottom();
nicklistadapter = new ArrayAdapter<String>(this, R.xml.mylistview, listofnicks.get(currentKeyWindows));
nicklist.setAdapter(nicklistadapter);
registerForContextMenu(nicklist);
channel=currentKeyWindows;
}