问题
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