1
好吧,所以我非常沮喪logcat。我創建了一個應用程序,它使用主要活動來託管兩個帶有片段的選項卡。帶有製表符和片段的ActionBar
我似乎無法弄清楚我的問題是什麼。我正在學習一個教程,它不起作用。
UPDATE-解決方法發佈如下。我在Tab接口方法中有錯誤的代碼。我在onTabSelected()中調用分離,因此片段管理器返回堆棧在初始化時始終爲空,在我的onTabReselected方法中,我試圖添加初始片段。非常愚蠢的錯誤。 - 感謝幫助推動我發現這個簡單的錯誤。
這裏是我的MainActivity:
package com.example.tabs;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the Tabs and Add them to the ActionBar
ActionBar.Tab tab1 = actionBar.newTab();
TabListener tab1Listener = new SimpleTabListener(this, Fragment1.class.getName());
tab1.setText("Fragment 1");
tab1.setTabListener(tab1Listener);
actionBar.addTab(tab1);
ActionBar.Tab tab2 = actionBar.newTab();
TabListener tab2Listener = new SimpleTabListener(this, Fragment2.class.getName());
tab2.setText("Fragment 2");
tab2.setTabListener(tab2Listener);
actionBar.addTab(tab2);
}
}
這裏是我的片段: 包com.example.tabs;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1, container, false);
}
} package com.example.tabs;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment2, container, false);
}
}
這裏是我的SimpleTabListener:
package com.example.tabs;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
public class SimpleTabListener implements ActionBar.TabListener {
Context m_context;
String m_fragmentClassName ;
Fragment m_fragment = null;
public SimpleTabListener(Context context, String tabFragmentClassName)
{
m_context = context;
m_fragmentClassName = tabFragmentClassName;
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
if(m_fragment == null)
{
m_fragment = Fragment.instantiate(m_context, m_fragmentClassName);
ft.add(R.id.contentContainer, m_fragment);
}else
{
ft.attach(m_fragment);
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
ft.detach(m_fragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
}
是否有人可以幫助我弄清楚爲什麼Fragment.instantiate方法不會建立我的片段?
這太令人沮喪了。 。 。
這裏是我的logcat
01-17 15:16:57.719: W/dalvikvm(6227): threadid=1: thread exiting with uncaught exception (group=0x40fc8930)
01-17 15:16:57.719: E/AndroidRuntime(6227): FATAL EXCEPTION: main
01-17 15:16:57.719: E/AndroidRuntime(6227): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread.access$700(ActivityThread.java:150)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.os.Looper.loop(Looper.java:175)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread.main(ActivityThread.java:5279)
01-17 15:16:57.719: E/AndroidRuntime(6227): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 15:16:57.719: E/AndroidRuntime(6227): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 15:16:57.719: E/AndroidRuntime(6227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-17 15:16:57.719: E/AndroidRuntime(6227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-17 15:16:57.719: E/AndroidRuntime(6227): at dalvik.system.NativeStart.main(Native Method)
01-17 15:16:57.719: E/AndroidRuntime(6227): Caused by: java.lang.NullPointerException
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.BackStackRecord.run(BackStackRecord.java:666)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.Activity.performStart(Activity.java:5292)
01-17 15:16:57.719: E/AndroidRuntime(6227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
01-17 15:16:57.719: E/AndroidRuntime(6227): ... 11 more
01-17 15:16:57.766: E/dalvikvm(6227): adjustAdaptiveCoef max=8388608, min=2097152, ut=368
您是否也可以發佈您的導入聲明? – Emmanuel
你可以嘗試在SimpleTabListener中將onTabSelected方法的空檢查?我的意思是:if(m_fragment!= null){ft.detach(m_fragment); } – hcelaloner
謝謝@hcelaloner你把我推回到那個班,讓我注意到我是多麼愚蠢。即使我讓該類實現接口的方法,我認爲他們是有序的。並且在每種方法中都有錯誤的代碼。 –