-2

The snippet below is not able to set the mRecyclerView

//package ..;

//import ..;

public class MyTransactions extends Activity {
    private RecyclerView mRecyclerView;
    private TransactionsHistoryAdapter mTransactionsHistoryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_transactions);
        initializeRecyclerView();
        fetchTransactions();
    }

    private void initializeRecyclerView() {
        mTransactionsHistoryAdapter = new TransactionsHistoryAdapter();
        mRecyclerView = (RecyclerView) findViewById(R.id.transaction_history_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(MyTransactions.this));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mTransactionsHistoryAdapter);
    }

    private void fetchTransactions() {..}

    private void populateWalletTransactions(String tr) {..}
}

And here is the resource R.layout.activity_my_transactions

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical">

    <!--<include layout="@layout/toolbar" />-->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/transaction_history_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:clipToPadding="false"
        android:paddingTop="16dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical" />

    <!--<include layout="@layout/view_marketplace_api" />-->

</LinearLayout>

when I start this activity from the main activity;

Intent ide = new Intent(MainActivity.this, MyTransactions.class);
ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ide);

A nullpointer exception is thrown in method initializeRecyclerView(); I am not sure what I am missing here.

Error Logs:

D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4170cd58)
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: wallet.daiscodemonks.com.bsmartpay, PID: 7098
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{wallet.daiscodemonks.com.bsmartpay/wallet.daiscodemonks.com.bsmartpay.MyTransactions}: java.lang.NullPointerException
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
                      at android.app.ActivityThread.access$800(ActivityThread.java:141)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5052)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.NullPointerException
                      at wallet.daiscodemonks.com.bsmartpay.MyTransactions.initializeRecyclerView(MyTransactions.java:45)
                      at wallet.daiscodemonks.com.bsmartpay.MyTransactions.onCreate(MyTransactions.java:23)
                      at android.app.Activity.performCreate(Activity.java:5242)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:141) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5052) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
                      at dalvik.system.NativeStart.main(Native Method) 

**The error is for the mRecyclerView, which is still null after the line mRecyclerView = (RecyclerView) findViewById(R.id.transaction_history_recyclerview);

Saurabh Bhoomkar
  • 543
  • 1
  • 8
  • 27
  • 1
    could you edit your question to include the logcat from the nullpointer exception? It should provide more information such as the exact line number where the exception is thrown – Bill Nov 18 '17 at 05:06
  • @bill logs added – Saurabh Bhoomkar Nov 18 '17 at 05:16
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vidhi Dave Nov 18 '17 at 05:16
  • 1
    The only thing I can think of is the directory of your layout file. Say the layout file is located in a layout folder specific to a certain device/dimension and you are using a device with different configuration. – Abbas Nov 18 '17 at 05:38
  • @SaurabhBhoomkar try to findview by id of recyclerview in oncreate method. before calling of initialize method. – Vidhi Dave Nov 18 '17 at 05:53
  • have you tried debugging with proper breakpoints? And please highlight lines 45 and 23 – Aswin P Ashok Nov 18 '17 at 06:06
  • @vishwa-dave That wouldn't work, aswin-p-ashok yes , my debug analysis is correct. See the last edit above – Saurabh Bhoomkar Nov 18 '17 at 06:25
  • please do check if you have used this id in any other layout it may not work try to change id name n give a try – vikrant arankalle Nov 18 '17 at 06:25
  • Do you have multiple layouts for different screen densities? – Aswin P Ashok Nov 18 '17 at 06:49

1 Answers1

0

put this,

   mRecyclerView = (RecyclerView)findViewById(R.id.transaction_history_recyclerview);
   mTransactionsHistoryAdapter = new TransactionsHistoryAdapter();
   RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
   mRecyclerView.setLayoutManager(mLayoutManager);
   recyclerView.setAdapter(mTransactionsHistoryAdapter);
Jinal Awaiya
  • 411
  • 2
  • 14