好的,所以我对logcat感到非常沮丧。我创建了一个Application,它使用一个主活动来托管带有片段的两个标签。
我似乎无法弄清楚我的问题是什么。我正在按照教程进行操作。
UPDATE-解决方案发布在下方。我在Tab接口方法中使用了错误的代码。我在onTabSelected()中调用detach因此片段管理器Back Stack在初始化时始终为null,而在我的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);
}
} 包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
答案 0 :(得分:0)
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{
if(m_fragment == null)
{
m_fragment = Fragment.instantiate(m_context, m_fragmentClassName);
ft.add(android.R.id.content, m_fragment);
}else
{
ft.attach(m_fragment);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{
// TODO Auto-generated method stub
if(m_fragment != null){
// TODO Auto-generated method stub
ft.detach(m_fragment);
}
}