I am working on a project which has many activities which uses recycler view in the exact way,just the data and onClickListener changes, I have the following abstract class for creating different activities with different onClickListener
public abstract class AbstractActivity extends ActionBarActivity implements View.OnClickListener{
private final String[] data;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
List<String> list;
Toolbar toolbar;
String title;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler);
Toolbar toolbar=(Toolbar)this.findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(title);
RecyclerView recyclerView=(RecyclerView)this.findViewById(R.id.recycler_view);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, null));
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
list.add("Hello");
adapter=new CardAdapter(list,id,this);
recyclerView.setAdapter(adapter);
}
public AbstractActivity(String title,String[] data,int id){
this.title=title;
this.data=data;
this.id=id;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_abstract, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public abstract void onClick(View v);
}
}
The recycler layout file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Syllabus">
<android.support.v7.widget.Toolbar android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:id="@+id/toolbar"
android:background="#03a9f4"
>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Now when i extend some class to create activity using the abstract class i get nullPointerException.The extend class is
public class Batch extends AbstractActivity {
public Batch(){
super("Hello",new String[]{"B.E Syllabus","B.Arch Syllabus","MBA Syllabus","MCA Syllabus","M.Tech Syllabus"},R.layout.main_page);
}
@Override
public void onClick(View v) {
}
}
logcat entry is
02-14 17:07:03.233 2807-2807/android.anoop.com.vtustudent E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{android.anoop.com.vtustudent/android.anoop.com.vtustudent.Batch}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.anoop.com.vtustudent.AbstractActivity.onCreate(AbstractActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5236)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
The toolbar in the recycler file is expanding fine and app runs perfectly if i remove the recyclerView from the layout file.Also if i use the same resource file in some activity directlyit works fine.I am getting the error only when i am using layout resource file from the abstract class. Why is this happening and how to correct this.Also is the way of abstract class the right way or is there some other way for creating same kind of activities. I am new to android programming and any help will be appreciated.Thank you