2012-11-04 86 views
1

我有兩種方法:ActionBarSherlock標籤零點異常

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; 
} 

第二種方法效果很好。

getSupportActionBar().addTab(newTab, true); 

我的AndroidManifest.xml:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

什麼做的第一方法(openPrivate)在該行結束由零點異常?爲什麼相同的代碼工作不同? 問候,丹尼斯。

的logcat:

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英尺):

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; 

} 
+0

您是否確定*您顯示的行是NPE?因爲ActionBar.Tab newTab = getSupportActionBar()。newTab();不支持NPE,所以getSupportActionBar不能爲null,newTab.setText(currentKeyWindows);不NPE所以newTab不能爲空。這條線上沒有任何東西可以導致NPE! – Simon

+0

不幸的是,我確定。如果你註釋掉這一行,該程序運行良好。 – Denis

+0

然後請發佈完整的logcat堆棧跟蹤。 – Simon

回答

0

從logcat的情況來看,似乎傳遞給您的適配器陣列是null

nicklistadapter = new ArrayAdapter<String>(this, R.xml.mylistview, listofnicks.get(currentKeyWindows)); 

我會驗證listofnicks.get(currentKeyWindows)返回一個有效的re SULT。

+0

我想給你提供一個加號,但是,不幸的是,我沒有權利:( – Denis

+0

@sam。如果我打擾看你的個人資料,看到13.8K,我會認爲它;) – Simon

+0

@Denis您可以點擊複選標記來表示這個問題已經解決(我們可以得到複選標記以及upvotes)。 – Sam