Activity inside library module starts but doesn't display layout

余生长醉 提交于 2020-01-25 21:39:32

问题


My code

My main project

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //         --------- 0 way
               View v = new com.sohaeb.mylibrary.test(this);
    setContentView(v);

   //         --------- 1 way

    startActivity(new Intent(this, com.sohaeb.mylibrary.MainActivity.class));

   //         --------- 2nd way

    Intent intent = new Intent();
    startActivity(intent.setClass(getApplicationContext(), com.sohaeb.mylibrary.MainActivity.class));

  //         --------- 3rd way
    Intent intent = new Intent();
    try {
        intent = new Intent(this, Class.forName("com.sohaeb.mylibrary.MainActivity"));
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    }   
 }

My module

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "test";


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

        Log.d(TAG, "onCreate: hellow ");
}
} 

Update #2

I have also followed this**tutorial here** which uses different approach:

inflate(context, R.layout.my_view, this);

But still ended with the same problem. Class launch but no layout


回答1:


The solution was because I have 2 xml layouts with the same name.

i.e:

  • Main project has main_activity.xml
  • Library Module also has main_activity.xml.

Android will ignore the second one.

Hope this helps anyone.



来源:https://stackoverflow.com/questions/45353701/activity-inside-library-module-starts-but-doesnt-display-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!