Reputation: 636
I am trying to open an activity which has recyclerView in it on the click of recyclerView of first activity. But now when I'm clicking the recyclerView app is crashing with the following message.
10-18 09:30:49.912 13018-13018/in.mumbaitravellers.mtleaders E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.mumbaitravellers.mtleaders, PID: 13018
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.mumbaitravellers.mtleaders/in.mumbaitravellers.mtleaders.activity.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at in.mumbaitravellers.mtleaders.activity.DetailActivity.setupRecycler(DetailActivity.java:120) at in.mumbaitravellers.mtleaders.activity.DetailActivity.onCreate(DetailActivity.java:52) at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 10-18 09:31:13.376 1801-2412/? E/native: do suspend false
The java code is as follows:
public class DetailActivity extends AppCompatActivity {
private ExpenseAdapter adapter;
private Realm realm;
private LayoutInflater inflater;
private FloatingActionButton fab;
private RecyclerView recycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
fab = (FloatingActionButton) findViewById(R.id.fab);
recycler = (RecyclerView) findViewById(R.id.recycler);
//get realm instance
this.realm = RealmController.with(this).getRealm();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*String event = getIntent().getStringExtra("eventName");
TextView tv = (TextView) findViewById(R.id.txt_EventName);
tv.setText(event);*/
setupRecycler();
if (!Prefs.with(this).getPreLoad()) {
setRealmData();
}
// refresh the realm instance
RealmController.with(this).refresh();
// get all persisted objects
// create the helper adapter and notify data set changes
// changes will be reflected automatically
setRealmAdapter(RealmController.with(this).getExpenses());
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = DetailActivity.this.getLayoutInflater();
View content = inflater.inflate(R.layout.activity_add_new_expense, null);
final EditText editExpense = (EditText) content.findViewById(R.id.edtxt_expense);
final EditText editDescription = (EditText) content.findViewById(R.id.edtxt_description);
AlertDialog.Builder builder = new AlertDialog.Builder(DetailActivity.this);
builder.setView(content)
.setTitle("Add Expense")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expense expense = new Expense();
expense.setId((int) (RealmController.getInstance().getExpenses().size() + System.currentTimeMillis()));
expense.setAmount(editExpense.getText().toString());
expense.setDescription(editDescription.getText().toString());
realm.beginTransaction();
realm.copyToRealm(expense);
realm.commitTransaction();
adapter.notifyDataSetChanged();
// scroll the recycler view to bottom
recycler.scrollToPosition(RealmController.getInstance().getExpenses().size() - 1);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
public void setRealmAdapter(RealmResults<Expense> expenses) {
RealmExpenseAdapter realmExpenseAdapter = new RealmExpenseAdapter(this.getApplicationContext(), expenses, true);
adapter.setRealmAdapter(realmExpenseAdapter);
adapter.notifyDataSetChanged();
}
private void setupRecycler() {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
recycler.setHasFixedSize(true);
// use a linear layout manager since the cards are vertically scrollable
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler.setLayoutManager(layoutManager);
// create an empty adapter and add it to the recycler view
adapter = new ExpenseAdapter(this);
recycler.setAdapter(adapter);
}
private void setRealmData() {
ArrayList<Expense> expenses = new ArrayList<>();
for (Expense e : expenses) {
// Persist your data easily
realm.beginTransaction();
realm.copyToRealm(e);
realm.commitTransaction();
}
Prefs.with(this).setPreLoad(true);
}
}
And the xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="in.mumbaitravellers.mtleaders.activity.DetailActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_detail" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_action_add" />
</android.support.design.widget.CoordinatorLayout>
Recycler View Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_expense"
style="@style/AppTheme.Card.Margins"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_small"
android:paddingBottom="@dimen/margin_normal"
android:paddingLeft="@dimen/margin_large"
android:paddingRight="@dimen/margin_large"
android:paddingTop="@dimen/margin_large"
android:textColor="#555555"
android:text="\u20B9"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="@+id/text_description"
android:textSize="20dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Content Detail.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerExpense"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="@layout/activity_detail"
tools:context="in.mumbaitravellers.mtleaders.activity.DetailActivity"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
What is wrong in code, please help. Thanks in advance!
Upvotes: 0
Views: 3502
Reputation: 1236
Your problem lies in incorrect search of RecyclerView
Change your code:
recycler = (RecyclerView) findViewById(R.id.recycler);
to
recycler = (RecyclerView) findViewById(R.id.recyclerExpense);
Upvotes: 1