简体   繁体   中英

ActionBar with Tabs and Fragments

Okay, so i'm extremely frustrated by logcat. I created an Application that uses a main activity to host two tabs with fragments.

I can't seem to figure out what my problem is. I am following a tutorial and it doesn't work.

UPDATE- Solution posted below. I had the wrong code in the Tab Interface methods. I was calling detach in onTabSelected() therefore the Fragment Manager Back Stack was always null on initialization, and in my onTabReselected method I was attempting to add the intial fragments. Very dumb mistake. -- Thanks for the help in pushing me to find this simple mistake.

Here is my 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);

}

}

Here are my Fragments: 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 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);

}

}

And here is my 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

}

}

Can someone please help me figure out Why the Fragment.instantiate Method will not build my fragments?

This is so frustrating. . .

Here is my 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

Wow i feel dumb for this one. so the solution was that I had the Wrong Tab Selection Code. I was implementing my tab code in the onTabReselected Method instead of onTabSelected

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM