TabListener不起作用

时间:2014-03-30 18:41:49

标签: java android tabs listener

我尝试在我的应用中实施标签并遵循本教程: http://android-er.blogspot.de/2012/06/create-actionbar-in-tab-navigation-mode.html

但现在我遇到的问题是我的TabListener没有从我的标签中获取参数。

tab1类:

package com.example.MainActivity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.MainActivity.R;

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
    return myFragmentView;
}

}

这是我的TabListener:

public class TabListener <T extends Fragment> implements ActionBar.TabListener{

private final Activity myActivity;
private final String myTag;
private final Class myClass;

public TabListener(Activity activity, String tag, Class<T> cls) {
    myActivity = activity;
    myTag = tag;
    myClass = cls;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

    // Check if the fragment is already initialized
    if (myFragment == null) {
        // If not, instantiate and add it to the activity
        myFragment = Fragment.instantiate(myActivity, myClass.getName());
        ft.add(android.R.id.content, myFragment, myTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(myFragment);
    }

}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

    if (myFragment != null) {
        // Detach the fragment, because another one is being attached
        ft.detach(myFragment);
    }

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

MainActivity:

public class MyActivity extends Activity {
    private String string1 = "gahwareawbad password gagaw";
    public String datum = new String("");
    //STUNDENPLAN-VARIABLEN









    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tabA = actionBar.newTab();
        tabA.setText("Tab A");
        tabA.setTabListener(new TabListener<Tab1>(this, "Tag A", Tab1.class));

        actionBar.addTab(tabA);
       Tab tabB = actionBar.newTab();
        tabB.setText("Tab B");
        tabB.setTabListener(new TabListener<Tab2>(this, "Tag B", Tab2.class));
        actionBar.addTab(tabB);

        Tab tabC = actionBar.newTab();
        tabC.setText("Tab C");
      tabC.setTabListener(new TabListener<Tab3>(this, "Tag C", Tab3.class));
        actionBar.addTab(tabC);

        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
       }


       // quellcode = (TextView) findViewById(com.example.MainActivity.R.id.TextView01);
        geschichte = (TextView) findViewById(com.example.MainActivity.R.id.geschichte);
        datum1 = (TextView) findViewById(com.example.MainActivity.R.id.datum1);

    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
      // TODO Auto-generated method stub
      super.onSaveInstanceState(outState);
        outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
    }





   }

MainXML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >
<TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/tabHost">
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
        </TabWidget>
        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <LinearLayout
                    android:id="@+id/einstellungen"
                    android:layout_width="fill_parent           android:layout_height="fill_parent" android:orientation="vertical"  android:background="#ffffff">
                 <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="Large Text"
                        android:id="@+id/datum1" android:layout_gravity="center_horizontal"
                        android:textColor="#000000"
                        android:background="#f6fff4"/>
                <TableLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">
                    <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" android:layout_column="0">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceMedium" android:text="Montag"
                                  android:id="@+id/montagx" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceMedium" android:text="Dienstag"
                                  android:id="@+id/dienstagx" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceMedium" android:text="Mittwoch"
                                  android:id="@+id/mittwochx" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceMedium" android:text="Donnerstag"
                                  android:id="@+id/donnerstagx" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceMedium" android:text="Freitag"
                                  android:id="@+id/freitagx" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/montag1" android:layout_column="0" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/dienstag1" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/mittwoch1" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/donnerstag1" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/freitag1" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/montag2" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/dienstag2" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/mittwoch2" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/donnerstag2" android:textColor="#000000"/>
                        <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                android:id="@+id/freitag2" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/montag3"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/dienstag3"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/mittwoch3"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/donnerstag3"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/freitag3"
                                  android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow2">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/montag4" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/dienstag4" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/mittwoch4" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/donnerstag4" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/freitag4" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow3">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/montag5"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/dienstag5"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/mittwoch5"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/donnerstag5"
                                  android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall" android:id="@+id/freitag5"
                                  android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow4">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/montag6" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/dienstag6" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/mittwoch6" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/donnerstag6" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/freitag6" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow5">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/montag7" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/dienstag7" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/mittwoch7" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/donnerstag7" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/freitag7" android:textColor="#000000"/>
                    </TableRow>
                    <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
                              android:id="@+id/tableRow6">
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/montag8" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/dienstag8" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/mittwoch8" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/donnerstag8" android:textColor="#000000"/>
                        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                                  android:textAppearance="?android:attr/textAppearanceSmall"
                                  android:id="@+id/freitag8" android:textColor="#000000"/>
                    </TableRow>
                </TableLayout>
            </LinearLayout>
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

            </LinearLayout>

            <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
            </LinearLayout>
            <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

03-30 22:09:51.720      441-876/com.android.launcher D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 692ms
03-30 22:09:51.997      441-877/com.android.launcher D/dalvikvm﹕ GC_FOR_ALLOC freed 1576K, 25% free 17107K/22564K, paused 176ms, total 189ms
03-30 22:09:52.027    2952-2952/? D/AndroidRuntime﹕ >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-30 22:09:52.057    2952-2952/? D/AndroidRuntime﹕ CheckJNI is ON
03-30 22:09:52.147    2952-2952/? D/dalvikvm﹕ Trying to load lib libjavacore.so 0x0
03-30 22:09:52.187    2952-2952/? D/dalvikvm﹕ Added shared lib libjavacore.so 0x0
03-30 22:09:52.227    2952-2952/? D/dalvikvm﹕ Trying to load lib libnativehelper.so 0x0
03-30 22:09:52.237    2952-2952/? D/dalvikvm﹕ Added shared lib libnativehelper.so 0x0
03-30 22:09:53.419      441-441/com.android.launcher I/Choreographer﹕ Skipped 55 frames!  The application may be doing too much work on its main thread.
03-30 22:09:53.542      302-338/system_process W/RecognitionManagerService﹕ no available voice recognition services found for user 0
03-30 22:09:54.067    2952-2952/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
03-30 22:09:54.077    2952-2952/? D/dalvikvm﹕ Note: class Landroid/app/ActivityManagerNative; has 157 unimplemented (abstract) methods
03-30 22:09:54.107      302-479/system_process I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MainActivity/.MyActivity} from pid 2952
03-30 22:09:54.147      302-479/system_process W/WindowManager﹕ Failure taking screenshot for (328x546) to layer 21005
03-30 22:09:54.187    2962-2962/com.example.MainActivity D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-30 22:09:54.217      302-739/system_process I/ActivityManager﹕ Start proc com.example.MainActivity for activity com.example.MainActivity/.MyActivity: pid=2962 uid=10048 gids={50048, 3003, 1028}
03-30 22:09:54.227    2952-2952/? D/AndroidRuntime﹕ Shutting down VM
03-30 22:09:54.257    2952-2956/? D/dalvikvm﹕ GC_CONCURRENT freed 92K, 17% free 491K/588K, paused 1ms+1ms, total 27ms
03-30 22:09:54.257    2952-2958/? D/jdwp﹕ Got wake-up signal, bailing out of select
03-30 22:09:54.257    2952-2958/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
03-30 22:09:54.427       36-201/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
03-30 22:09:55.067    2962-2962/com.example.MainActivity E/Trace﹕ error opening trace file: No such file or directory (2)
03-30 22:09:57.057    2962-2962/com.example.MainActivity D/AndroidRuntime﹕ Shutting down VM
03-30 22:09:57.057    2962-2962/com.example.MainActivity W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-30 22:09:57.077    2962-2962/com.example.MainActivity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MainActivity/com.example.MainActivity.MyActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.MainActivity.MyActivity.onCreate(MyActivity.java:104)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
03-30 22:09:57.087      302-739/system_process W/ActivityManager﹕ Force finishing activity com.example.MainActivity/.MyActivity
03-30 22:09:57.117      302-739/system_process W/WindowManager﹕ Failure taking screenshot for (328x546) to layer 21010
03-30 22:09:57.397       36-490/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
03-30 22:09:57.637      302-319/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{40e9e8c0 u0 com.example.MainActivity/.MyActivity}
03-30 22:09:59.227      302-329/system_process I/Choreographer﹕ Skipped 30 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:1)

使用MainActivity中的导入问题

更改此

import android.app.ActionBar.TabListener;

import com.example.MainActivity.TabListener

或更改自定义TabListener类名。