Android studio Java Recycler 启动时出现视图错误
Android studio Java Recycler view error on launch
对不起我的英语我是法国人:)
我正在尝试学习如何在 Android studio 中编写代码,我从 Recycler 视图开始,不幸的是我遇到了一个错误,我不知道如何修复它。
提前感谢您的帮助
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mtoapp, PID: 4393
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mtoapp/com.example.mtoapp.Recycler.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3602)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at com.example.mtoapp.Recycler.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1312)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3602)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
MainActivity.java(所有代码)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public MyAdapter(Context ct, String s1[], String s2[], int img[]){
context = context;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView myText1, myText2;
ImageView myImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.myText1);
myText2 = itemView.findViewById(R.id.myText2);
myImage = itemView.findViewById(R.id.myImageView);
}
}
}
MainActivity 中的错误部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find ID
----> recyclerView.findViewById(R.id.recyclerView);
//String S1 et S2
s1 = getResources().getStringArray(R.array.days);
s2 = getResources().getStringArray(R.array.description);
link 的外部代码:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.mtoapp.R;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public MyAdapter(Context ct, String s1[], String s2[], int img[]){
context = context;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView myText1, myText2;
ImageView myImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.myText1);
myText2 = itemView.findViewById(R.id.myText2);
myImage = itemView.findViewById(R.id.myImageView);
}
}
}
再次感谢您,祝您有愉快的一天:)
首先你应该启动 recyclerview
像:
RecyclerView recyclerview = findViewById(R.id.recycler_view_id);
那么你可以使用recyclerview.findViewById()
对不起我的英语我是法国人:)
我正在尝试学习如何在 Android studio 中编写代码,我从 Recycler 视图开始,不幸的是我遇到了一个错误,我不知道如何修复它。
提前感谢您的帮助
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mtoapp, PID: 4393
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mtoapp/com.example.mtoapp.Recycler.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3602)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View androidx.recyclerview.widget.RecyclerView.findViewById(int)' on a null object reference
at com.example.mtoapp.Recycler.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1312)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3602)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
MainActivity.java(所有代码)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public MyAdapter(Context ct, String s1[], String s2[], int img[]){
context = context;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView myText1, myText2;
ImageView myImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.myText1);
myText2 = itemView.findViewById(R.id.myText2);
myImage = itemView.findViewById(R.id.myImageView);
}
}
}
MainActivity 中的错误部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Find ID
----> recyclerView.findViewById(R.id.recyclerView);
//String S1 et S2
s1 = getResources().getStringArray(R.array.days);
s2 = getResources().getStringArray(R.array.description);
link 的外部代码:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.mtoapp.R;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
String data1[], data2[];
int images[];
Context context;
public MyAdapter(Context ct, String s1[], String s2[], int img[]){
context = context;
data1 = s1;
data2 = s2;
images = img;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.myText1.setText(data1[position]);
holder.myText2.setText(data2[position]);
holder.myImage.setImageResource(images[position]);
}
@Override
public int getItemCount() {
return images.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView myText1, myText2;
ImageView myImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
myText1 = itemView.findViewById(R.id.myText1);
myText2 = itemView.findViewById(R.id.myText2);
myImage = itemView.findViewById(R.id.myImageView);
}
}
}
再次感谢您,祝您有愉快的一天:)
首先你应该启动 recyclerview
像:
RecyclerView recyclerview = findViewById(R.id.recycler_view_id);
那么你可以使用recyclerview.findViewById()