- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我的应用程序遇到问题,当我按下名为“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/
我收到多行错误,指出它有重复的类 Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCom
我的项目昨天运行良好,今天却出现以下错误。请帮忙。 出了什么问题:无法解析配置“:app:debugCompileClasspath”的所有文件。 Could not find support-v4.
错误:配置项目“:app”时出现问题。 Could not find support-v4.jar (com.android.support:support-v4:24.0.0). Searched
我正在使用 react-native-webrtc 运行示例应用程序模块。我正在尝试在 android 中运行该应用程序,但出现这样的错误... * What went wrong: A probl
错误:配置项目“:app”时出现问题。 Could not find support-v4.jar (com.android.support:support-v4:24.0.0). Searched
我克隆了我的项目并使用 Android studio 打开,但构建失败并出现以下错误并且同一个项目在其他项目上运行良好。 出了什么问题:无法解析配置“:app:flavorUnsignedReleas
我正在运行示例应用程序......我正在使用 this模块.....但是我收到这样的错误... FAILURE: Build failed with an exception. * What went
我有一个 Ionic 项目,在升级到 OSx Mojave 后,出于某种原因不再构建。 该项目基于 Ionic 3 构建。 我有 Cordova CLI 7.0.0 当我运行时 ionic cordo
我有一个 Ionic 项目,在升级到 OSx Mojave 后,出于某种原因不再构建。 该项目基于 Ionic 3 构建。 我有 Cordova CLI 7.0.0 当我运行时 ionic cordo
添加 Android 平台(ionic cordova platform add android)后,我构建了我的 Ionic 项目(ionic cordova build android),但出现错
这个问题在这里已经有了答案: Manifest merger failed : Attribute application@appComponentFactory - Androidx (14 个答
关于我的 Gradle 文件中的以下“complie”: dependencies { compile 'com.android.support:support-v4:25.3.1' }
有些困惑。 我想更频繁地使用@supports 但是...我不关心浏览器如何看待它。希望您能说清楚,如果可以的话谢谢您。 如果浏览器本身不理解@supports,它如何计算@support 'not'
我刚刚升级到 Dart 2 和最新版本的 Flutter,现在我无法构建我的应用程序。我在互联网上环顾四周,但仍然不明白为什么会发生这种情况。 我得到的错误是: FAILURE: Build fail
我正在学习 Firebase,但由于以下错误而陷入困境: Error:Failed to resolve: com.android.support:customtabs:25.4.0 Error:Fa
我在构建本周时出于某种原因尝试使用Cordova进行项目时遇到问题: cordova build android gradle获取com.android.support:support alpha而不
对应的androidx是什么com.android.support:support-compat 的图书馆实现push notifications ? 他们说here它是 androidx 的一部分,
我正在尝试为我的应用设置插桩单元测试。并且我已根据以下开发者站点链接添加了依赖项。 https://developer.android.com/training/testing/unit-testin
当我想使用 Proguard 规则生成签名的 APK(发布)时,我收到了以下错误消息: Cannot find a version of 'com.android.support:support-an
这个问题在这里已经有了答案: Failed to resolve: com.android.support:appcompat-v7:26.0.0 (14 个答案) Failed to resolv
我是一名优秀的程序员,十分优秀!