gpt4 book ai didi

java - 尝试调用虚拟方法 Android Studio RecyclerView android.support.v7

转载 作者:行者123 更新时间:2023-12-02 11:52:25 24 4
gpt4 key购买 nike

您好,我的应用程序遇到问题,当我按下名为“Riders”的按钮时,它就会崩溃。我收到的错误是:

 12-13 10:40:03.467 16463-16463/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 16463
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: 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:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
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 bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

多年来我一直在尝试解决这个问题。该错误引用了名为 RiderActivity 的页面的错误:

package bskerritt.student.ncirl.ie.saddleup;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class RiderActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Contact> list = new ArrayList<Contact>();

int[] image_id = { R.drawable.lilyheadshot, R.drawable.aaronheadshot, R.drawable.willowheadshot,
R.drawable.kidheadshot1, R.drawable.kidheadshot2, R.drawable.kidheadshot3, R.drawable.kidheadshot4,
R.drawable.kidheadshot5 };

String[] name, mobile, address;

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

name = getResources().getStringArray(R.array.rider_name);
mobile = getResources().getStringArray(R.array.rider_mobile);
address = getResources().getStringArray(R.array.rider_address);

int count = 0;

for(String Name: name) {

Contact contact = new Contact(image_id[count], Name, mobile[count], address[count]);
count++;
list.add(contact);
}

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new ContactAdapter(list);
recyclerView.setAdapter (adapter);
}
}

我正在使用 android studio,任何帮助将不胜感激。我尝试了 stackoverflow 上的一些解决方案,但没有成功。请帮忙。谢谢 !

这是我的activity_rider的代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RecyclerView">

<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>

<ImageView
android:id="@+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lilyheadshot" />

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="@+id/rider_name"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rider_name"
android:layout_toRightOf="@id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="@+id/rider_mobile"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rider_mobile"
android:layout_toRightOf="@id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="@+id/rider_address"
/>

</RelativeLayout>


</android.support.v7.widget.RecyclerView>


<Button
android:id="@+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="@string/addRiderBtn"
android:textColor="@color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />

另外,关于提供上下文的其他代码,我收到此错误:http://prntscr.com/hmw2yi

好的,第一个错误已经修复,更改 R.id.RecyclerView 就可以了,但由于某种原因,错误现在发生在不同的行上:

12-13 11:10:11.590 2486-2486/bskerritt.student.ncirl.ie.saddleup E/AndroidRuntime: FATAL EXCEPTION: main
Process: bskerritt.student.ncirl.ie.saddleup, PID: 2486
java.lang.RuntimeException: Unable to start activity ComponentInfo{bskerritt.student.ncirl.ie.saddleup/bskerritt.student.ncirl.ie.saddleup.RiderActivity}: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #15: RecyclerView has no LayoutManager
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3996)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:860)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at bskerritt.student.ncirl.ie.saddleup.RiderActivity.onCreate(RiderActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
12-13 11:11:00.875 2486-2494/bskerritt.student.ncirl.ie.saddleup W/art: Suspending all threads took: 6.024ms

最佳答案

使用这个

recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);

而不是这个

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

编辑

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bskerritt.student.ncirl.ie.saddleup.RiderActivity">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RecyclerView"/>

<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
>

<ImageView
android:id="@+id/rider_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lilyheadshot" />

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/rider_image"
android:text="Lily Skerritt"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="@+id/rider_name"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rider_name"
android:layout_toRightOf="@id/rider_image"
android:text="0872287760"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="@+id/rider_mobile"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/rider_mobile"
android:layout_toRightOf="@id/rider_image"
android:text="Laytown, Meath"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="@+id/rider_address"
/>

</RelativeLayout>





<Button
android:id="@+id/button5"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:text="@string/addRiderBtn"
android:textColor="@color/suPrimary"
android:textSize="18sp"
android:textStyle="bold" />

关于java - 尝试调用虚拟方法 Android Studio RecyclerView android.support.v7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com