我有一个Android库项目my_lib
基本上my_lib
list activity
说MyListActivity
它有资源和东西。当我作为Android应用程序运行' MyListActivity'工作正常。
但是当我创建另一个proj
说test_proj
并将my_lib
作为android库项目并添加到test_proj
作为库并在MyListActivity
中添加AndroidManifest
个活动时{1}}并在NullPoiterException
中运行MyListActivity
,即findViewById(R.id.list)
返回null。我该如何解决这个问题
代码如下
onCreate Of MyListActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
initData();
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i], packageNames[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
onCreate of MainActivity of test proj
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, ImageTextListViewActivity.class));
}
错误日志如下
D/AndroidRuntime( 778): Shutting down VM
E/AndroidRuntime( 778): FATAL EXCEPTION: main
E/AndroidRuntime( 778): java.lang.NoSuchFieldError: com.mass.applist.R$id.list
E/AndroidRuntime( 778): at com.mass.applist.MyListActivity.onCreate(MyListActivity.java:55)
E/AndroidRuntime( 778): at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime( 778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime( 778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime( 778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime( 778): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime( 778): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 778): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 778): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime( 778): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 778): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime( 778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime( 778): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
的setContentView(R.layout.activity_main);
确保lib中的布局和您的示例活动没有相同的名称。