Question

I have an android library project my_lib basically my_lib has list activity say MyListActivity it has resources and stuffs. when I run as android application 'MyListActivity' works fine.

but when I create another proj say test_proj and make my_lib as android library project and add to test_proj as library and added MyListActivity activity in AndroidManifest and run I am getting NullPoiterException in MyListActivity, i.e at findViewById(R.id.list) is returning null. how can I fix this

Code is as below

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));

}

Error Log is as below

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)
Was it helpful?

Solution

setContentView(R.layout.activity_main);

make sure layout in lib and your sample activity does not have same name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top