无法初始化Android RecyclerView

时间:2017-11-18 05:00:36

标签: android android-recyclerview

以下代码段无法设置//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) {..} }

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>

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

当我从主要活动开始这项活动时;

initializeRecyclerView();

方法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)  中抛出了一个nullpointer异常 我不确定我在这里缺少什么。

错误日志:

mRecyclerView = (RecyclerView) findViewById(R.id.transaction_history_recyclerview);

**错误是针对mRecyclerView,在行$q

之后仍然为空

1 个答案:

答案 0 :(得分:0)

把它,

   mRecyclerView = (RecyclerView)findViewById(R.id.transaction_history_recyclerview);
   mTransactionsHistoryAdapter = new TransactionsHistoryAdapter();
   RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
   mRecyclerView.setLayoutManager(mLayoutManager);
   recyclerView.setAdapter(mTransactionsHistoryAdapter);