如何在我的RecyclerView应用程序中修复NullPointerException?

时间:2019-01-11 20:18:10

标签: android android-studio nullpointerexception nullreferenceexception

我正在创建一个应用程序,该应用程序将使用Firebase通过注册和身份验证显示宠物的示例列表。

我使用了一个教程,在其中一项活动中创建了RecyclerView。不幸的是,我没有遵循如何解决此NullPointerException ...

您能告诉我我应该怎么做,并解释这里的错误吗?

预先感谢, 祝你有美好的一天。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.firebaseloginapp, PID: 5865
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebaseloginapp/com.firebaseloginapp.AccountActivity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
    at com.firebaseloginapp.AccountActivity.MainActivity.onCreate(MainActivity.java:56)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

MainActivity:

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView; 
private RecyclerView.Adapter mAdapter; 
private RecyclerView.LayoutManager mLayoutManager;


private Button btnChangePassword, btnRemoveUser,
        changePassword, remove, signOut, btn_Breeding, btn_Adoption;
private TextView email;

private EditText oldEmail, password, newPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<ExampleItem> exampleList = new ArrayList<>();
    exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 1", "Line 2"));


    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); //xml
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new ExampleAdapter(exampleList);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);   

0 个答案:

没有答案