我有一个android库项目my_lib,基本上my_liblist activity,说MyListActivity它有资源和东西。当我作为android应用程序运行时,“MyListActivity”工作正常。

但是当我创建另一个proj并说test_proj并将my_lib作为android库项目并添加到test_proj作为库并在MyListActivity中添加了AndroidManifest Activity 并运行时,我在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);

}

测试项目MainActivity的onCreate
@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)

最佳答案

setContentView(R.layout.activity_main);

确保lib中的布局和您的示例 Activity 没有相同的名称。

08-17 01:04