Activity inside library module starts but doesn

2019-08-23 14:34发布

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条回答
Deceive 欺骗
2楼-- · 2019-08-23 14:48

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.

查看更多
登录 后发表回答