Search code examples
androidandroid-fragmentsandroid-fragmentactivityfragmentmanager

Display two fragments on a tablet screen


I want to host two fragments at the same time, but so far my code isn't working.

This is my specific problem and i don't kow what is wrong, and why it won't start.

Here is my code:

<?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" />

My main activity:

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

And fragment classes

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

And 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;
}         

This is my first time working with fragments, so i need help. Thanks!!

EDIT - Error code

There isn't any logcat error output regarding my application, and console output is like this...

[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!

Solution

  • This is weird. There were some other also facing this issue. Check these questions, it might help you

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

    Installing error: Unkown failure (android emulator)