在平板电脑屏幕上显示两个片段

时间:2014-12-22 14:51:56

标签: android android-fragments android-fragmentactivity fragmentmanager

我想同时托管两个片段,但到目前为止我的代码还没有工作。

这是我的具体问题,我不知道什么是错的,以及为什么它不会开始。

这是我的代码:

<?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:baselineAligned = "false"
android:orientation="horizontal"
android:id="@+id/frags">
<!-- "Fragment A" -->
<FrameLayout
        android:id="@+id/containter"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
<!-- "Fragment B" -->
<FrameLayout
        android:id="@+id/containerDetails"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我的主要活动:

    public class MainActivity extends FragmentActivity {

    Fragment frag,frag2;
    FragmentManager fm;


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

        String screen = getString(R.string.screen_type);

        /*
         * Get a reference to fragment manager
         * Wire the container to represent fragment
         */
        fm = getSupportFragmentManager();
        frag = fm.findFragmentById(R.id.container);
        if(screen.equals("large")){
            frag2 = fm.findFragmentById(R.id.containerDetails);
            loadFragments(frag,frag2,fm);
        }
        /*Loads the fragment into the activity*/
        else
        loadFragment(frag,fm);

    }

    private void loadFragments(Fragment frag, Fragment frag2, FragmentManager fm) {
        if(frag == null && frag2 == null){

            frag = new DisplayFragment();
            frag2 = new DetailsFragment();
            fm.beginTransaction().add(R.id.container,frag).add(R.id.containerDetails, frag2).commit();

        }   
    }

    private void loadFragment(Fragment frag, FragmentManager fm) {

        if(frag == null){

            frag = new DisplayFragment();
            fm.beginTransaction().add(R.id.container,frag).commit();

        }
    }

    public void updateDetails(int position) {
        fm.beginTransaction().detach(frag2);
        frag2 = new DetailsFragment();
        Bundle b = new Bundle();
        b.putInt("Id",position);
        frag2.setArguments(b);
        fm.beginTransaction().add(R.id.containerDetails, frag2).commit();
    }
}

片段类

public class DisplayFragment extends Fragment implements OnClickListener, OnItemClickListener {

Button mNew;
ImageView mSettings;
ListView mList;
ArrayList<Task> mTasks;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_main, parent, false);

    setInterface(v);

    return v;
}
/*when item is clicked pass the task to a new activity*/
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    String screen = getString(R.string.screen_type);
    if(screen.equals("large")){
        ((MainActivity) getActivity()).updateDetails(mTasks.get(position).getId());
    }
    Intent i = new Intent(getActivity(),DetailsActivity.class);
    Bundle b = new Bundle();
    b.putInt("Id",mTasks.get(position).getId());
    i.putExtra("Task",b);
    startActivity(i);
    getActivity().finish();
}

DetailsFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_details, parent, false);

    Bundle b = getArguments();
    if(b!=null){
        db = new DBHelper(getActivity().getApplicationContext());
        dbA = new DBHelperAlarm(getActivity().getApplicationContext());
            mTask =db.getTask(b.getInt("Id")); 
            if(mTask == null){
                mTask = db.getTask(UUID.fromString(b.getString("UUID")));
            }
                if(mTask.getAudio()){
                String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+mTask.getUuid().toString()+".3gp";
                mMusic = MediaPlayer.create(getActivity(),Uri.parse(path));
            }
    }
    else{
        return v;
    }

    setInterface(v);

    return v;
}         

这是我第一次使用片段,所以我需要帮助。谢谢!!

编辑 - 错误代码

关于我的应用程序没有任何logcat错误输出,控制台输出是这样的......

[2014-12-22 15:32:37 - To-DoList] Installing To-DoList.apk...
[2014-12-22 15:32:37 - To-DoList] Installation error: Unknown failure
[2014-12-22 15:32:37 - To-DoList] Please check logcat output for more details.
[2014-12-22 15:32:37 - To-DoList] Launch canceled!

2 个答案:

答案 0 :(得分:1)

这很奇怪。 还有一些人也面临这个问题。 检查这些问题,它可能对您有所帮助

android: Installation Error:Unknown Failure-while running an apk file in

Installing error: Unkown failure (android emulator)

答案 1 :(得分:-2)

你的&#34; FragmentB&#34;由于android:layout_width =&#34; match_parent&#34;,FrameLayout填满了屏幕。你应该使用android:layout_width =&#34; 0dp&#34;使用android:layout_weight =&#34; 1&#34;

<?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:baselineAligned = "false"
android:orientation="horizontal"
android:id="@+id/frags">
<!-- "Fragment A" -->
<FrameLayout
    android:id="@+id/containter"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"/>
<!-- "Fragment B" -->
<FrameLayout
    android:id="@+id/containerDetails"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent" />