在Android中切换选项卡时两个布局重叠

时间:2013-06-26 19:57:46

标签: android

我遇到了障碍,我的标签式布局与其内容重叠。我目前正在创建一个Android应用。这是第一个选项卡打开时。 (链接到下面的图片):

http://oi39.tinypic.com/330r8nq.jpg

当我打开第二个标签时会发生这种情况(链接到下面的图片):

http://oi39.tinypic.com/9fzl02.jpg

您可能会注意到“Button”按钮与MainActivity重叠。那是我的问题。我不希望它们重叠。我只想让按钮显示其他活动(没有重叠)。这是我的MainActivity.java代码的一部分:

package com.example.currencyconverter;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ActionBar actionBar = getActionBar();
    actionBar.setSubtitle("Created By Rohit Nandakumar");
    actionBar.setTitle("Currency Converter"); 
    // Specify that tabs should be displayed in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab Frag1 = actionBar.newTab().setText("Converter");
    ActionBar.Tab Frag2 = actionBar.newTab().setText("Currencies");
    ActionBar.Tab Frag3 = actionBar.newTab().setText("News");

    Fragment fragment1 = new Fragment1();
    Fragment fragment2 = new Fragment2();
    Fragment fragment3 = new Fragment3();

    Frag1.setTabListener(new MyTabsListener(fragment1));
    Frag2.setTabListener(new MyTabsListener(fragment2));
    Frag3.setTabListener(new MyTabsListener(fragment3));

    actionBar.addTab(Frag1);
    actionBar.addTab(Frag2);
    actionBar.addTab(Frag3);

这是Fragment1.java代码:

package com.example.currencyconverter;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main, container, false);
}
}

这是我的Fragment2.java代码:

    package com.example.currencyconverter;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_first, container, false);
}
}

这是我的MyTabsListener.java文件:

    package com.example.currencyconverter;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Fragment;
import android.app.FragmentTransaction;

public class MyTabsListener implements TabListener {

    public Fragment fragment;

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




    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);

    }

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

    }



}

有什么问题?我究竟做错了什么。我搜索了谷歌,但我找不到解决方案。非常感谢任何帮助。

这是我的Logcat:

06-26 21:18:38.394: E/AndroidRuntime(2890): FATAL EXCEPTION: main
06-26 21:18:38.394: E/AndroidRuntime(2890): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.currencyconverter/com.example.currencyconverter.MainActivity}: java.lang.IllegalStateException: commit already called
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.os.Looper.loop(Looper.java:137)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at dalvik.system.NativeStart.main(Native Method)
06-26 21:18:38.394: E/AndroidRuntime(2890): Caused by: java.lang.IllegalStateException: commit already called
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at   android.app.BackStackRecord.commit(BackStackRecord.java:574)  
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:575)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at com.example.currencyconverter.MainActivity.onCreate(MainActivity.java:75)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.Activity.performCreate(Activity.java:5104)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-26 21:18:38.394: E/AndroidRuntime(2890):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-26 21:18:38.394: E/AndroidRuntime(2890):     ... 11 more
06-26 21:18:46.173: I/Process(2890): Sending signal. PID: 2890 SIG: 9
06-26 21:19:07.554: E/Trace(2944): error opening trace file: No such file or directory (2)
06-26 21:19:08.233: D/dalvikvm(2944): GC_CONCURRENT freed 152K, 11% free 2619K/2912K, paused 71ms+3ms, total 138ms

如果我添加提交,该方法如下所示:

@Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment).commit();
        }

0 个答案:

没有答案