0

我正在为我的 android 应用程序处理片段,我使用 actionbarsherlock。当我尝试在模拟器中运行我的应用程序时,按钮不会出现,只出现片段选项卡。

主要的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <FrameLayout 
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

片段_1 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <Button
        android:id="@+id/btn1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/song1"
        android:onClick="buttonClicked1" 
        />

    <Button
        android:id="@+id/btn2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/song2"
        android:onClick="buttonClicked2"
         />

    <Button
        android:id="@+id/btn3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/song3"
        android:onClick="buttonClicked3" />

    <Button
        android:id="@+id/btn4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/song4"
        android:onClick="buttonClicked4" />

    <Button
        android:id="@+id/btn5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/song5" 
        android:onClick="buttonClicked5"
        />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn5"
        android:text="play1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:text="play2" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:text="play3" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:text="play4" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText4"
        android:text="play5" />


</LinearLayout>

主要活动代码:

public class MainActivity extends SherlockFragmentActivity {

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

        ActionBar actionbar = getSupportActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.setTitle("Dancing Fountain");

        ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Song List");
        ActionBar.Tab Frag2Tab = actionbar.newTab().setText("About");


        Fragment Fragment1 = new Fragment_1();
        Fragment Fragment2 = new Fragment_2();



        Frag1Tab.setTabListener(new MyTabsListener(Fragment1));
        Frag2Tab.setTabListener(new MyTabsListener(Fragment2));



        actionbar.addTab(Frag1Tab);
        actionbar.addTab(Frag2Tab);



    }

    class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment){
            this.fragment = fragment;
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.replace(R.id.fragment_container, fragment);


        }

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

        }

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

        }


    }



}

Fragment_1 代码:

public class Fragment_1 extends SherlockFragment  {

    private EditText textField1;
    private EditText textField2;
    private EditText textField3;
    private EditText textField4;
    private EditText textField5;
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.fragment_1, container, false); 

        button1 = (Button)v.findViewById(R.id.btn1);
        button2 = (Button)v.findViewById(R.id.btn2);
        button3 = (Button)v.findViewById(R.id.btn3);
        button4 = (Button)v.findViewById(R.id.btn4);
        button5 = (Button)v.findViewById(R.id.btn5);

        textField1 = (EditText)v.findViewById(R.id.editText1);
        textField2 = (EditText)v.findViewById(R.id.editText2);
        textField3 = (EditText)v.findViewById(R.id.editText3);
        textField4 = (EditText)v.findViewById(R.id.editText4);
        textField5 = (EditText)v.findViewById(R.id.editText5);

        textField1.setVisibility(View.GONE);
        textField2.setVisibility(View.GONE);
        textField3.setVisibility(View.GONE);
        textField4.setVisibility(View.GONE);
        textField5.setVisibility(View.GONE);

        return v;



        }

    public void buttonClicked1(View v){
        Log.d("MyTag", "The song has been successfully added!");

        new asynctask().execute(textField1);

    }
    public void buttonClicked2(View v) {
        Log.d("MyTag", "The song has been successfully added!");

        new asynctask().execute(textField2);
    }
    public void buttonClicked3(View v) {
        Log.d("MyTag", "The song has been successfully added!");

        new asynctask().execute(textField3);
    }
    public void buttonClicked4(View v) {
        Log.d("MyTag", "The song has been successfully added!");

        new asynctask().execute(textField4);
    }
    public void buttonClicked5(View v) {

        Log.d("MyTag", "The song has been successfully added!");
        new asynctask().execute(textField5);
    }

    }

这是我的Logcat:

09-12 02:33:59.954: D/AndroidRuntime(1158): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
09-12 02:33:59.954: D/AndroidRuntime(1158): CheckJNI is ON
09-12 02:34:01.824: D/AndroidRuntime(1158): Calling main entry com.android.commands.pm.Pm
09-12 02:34:01.914: D/AndroidRuntime(1158): Shutting down VM
09-12 02:34:01.944: I/AndroidRuntime(1158): NOTE: attach of thread 'Binder Thread #3' failed
09-12 02:34:01.944: D/dalvikvm(1158): GC_CONCURRENT freed 101K, 78% free 462K/2048K, paused 3ms+6ms
09-12 02:34:01.964: D/jdwp(1158): Got wake-up signal, bailing out of select
09-12 02:34:01.964: D/dalvikvm(1158): Debugger has detached; object registry had 1 entries
09-12 02:34:02.704: D/AndroidRuntime(1171): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
09-12 02:34:02.704: D/AndroidRuntime(1171): CheckJNI is ON
09-12 02:34:03.944: D/AndroidRuntime(1171): Calling main entry com.android.commands.am.Am
09-12 02:34:03.994: I/ActivityManager(88): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.timskie.dancingfountain/.MainActivity} from pid 1171
09-12 02:34:04.016: D/AndroidRuntime(1171): Shutting down VM
09-12 02:34:04.044: I/AndroidRuntime(1171): NOTE: attach of thread 'Binder Thread #3' failed
09-12 02:34:04.044: D/dalvikvm(1171): GC_CONCURRENT freed 102K, 77% free 483K/2048K, paused 2ms+2ms
09-12 02:34:04.054: D/jdwp(1171): Got wake-up signal, bailing out of select
09-12 02:34:04.054: D/dalvikvm(1171): Debugger has detached; object registry had 1 entries
09-12 02:34:25.274: D/dalvikvm(166): GC_CONCURRENT freed 384K, 6% free 10331K/10951K, paused 73ms+24ms
4

1 回答 1

0

检查这个例子:

http://wptrafficanalyzer.in/blog/adding-navigation-tabs- contains-listview-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/

这解释了 sherlcok 片段和添加标签的正确用法。

于 2013-09-12T07:20:41.190 回答