如何在MainActivity中为不同的屏幕尺寸实现两个布局

时间:2018-07-14 19:40:25

标签: android android-layout fragment android-screen-support android-layout-weight

我正在遵循Udacity的课程,最后在完成过程中遇到了这个错误。

您还可以通过单击here来检查Udacity的代码 以及我的布局文件12

**

  

谢谢。

**

com.thelazyprogrammer.harshit.mycustomapps.ui.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:6998)

此错误指向setContentView(R.layout.activity_main);

但是不知道缺少了什么。 我的代码:-

 public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener
{
    // Variable to store the values of list index of selected image.
    private int headIndex,
    bodyIndex,
    legIndex;

    private boolean mTwoPane;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
   /*<---THE ABOVE ERROR POINTING BELOW LINE'S CODE--->*/
        setContentView(R.layout.activity_main); 
   /*<------------------------------------------------>*/

        /*Intent intent = new Intent(this, AndroidMeActivity.class);
        startActivity(intent);*/

        if(findViewById(R.id.android_me_linear_layout) != null)
        {
            mTwoPane=true;


            // Setting grid view into two columns.
            GridView gridView = (GridView) findViewById(R.id.images_grid_view);
            gridView.setNumColumns(2);

            // Getting rid of the button in the tablet mode.
            Button nextButton = (Button) findViewById(R.id.next_button);
            nextButton.setVisibility(View.GONE);

if(savedInstanceState!=null)
{
    FragmentManager fragmentManager = getSupportFragmentManager();

    BodyClassFragment headFragment, bodyFragment, legFragment;

    // Creating new head, body, leg.
    headFragment = new BodyClassFragment();
    headFragment.setImageIds(AndroidImageAssets.getHeads());

    bodyFragment = new BodyClassFragment();
    bodyFragment.setImageIds(AndroidImageAssets.getBodies());

    legFragment = new BodyClassFragment();
    legFragment.setImageIds(AndroidImageAssets.getLegs());

    // Adding fragment to it's container.
    fragmentManager.beginTransaction()
            .add(R.id.head_container, headFragment)
            .add(R.id.body_container, bodyFragment)
            .add(R.id.leg_container, legFragment)
            .commit();
}
        }
        else
        {
            mTwoPane=false;
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您必须从Google Docs.

了解显示尺寸的基本知识