我是新的Android开发者,因为应用程序在运行时崩溃而面临问题。请检查下面的代码。
**MyAdapter.java**
我是android中Recycler view的新手,我正在设计文本的样式,所以我做了如下的工作,这是我的适配器类。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
//variables
private List<Model> modelList;
private Context context;
//constructor
public MyAdapter(List<Model> modelList, Context context) {
this.modelList = modelList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//this method will call whenever our view holder is created
// inflate the row.xml
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
if(view == null){
Log.i(view.toString(), "View is null");
}else{
Log.i(view.toString(), "View is not null");
}
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// this method will bind the data to viewHolder from when it will be shown to other view
Model model = modelList.get(position);
if(holder.team1Tv == null){
Log.i("model.getTeam1() -->",model.getTeam1());
}else{
Log.i("model.getTeam2() -->",model.getTeam1());
}
holder.team1Tv.setText(model.getTeam1());
holder.team2Tv.setText(model.getTeam2());
holder.matchTypeTv.setText(model.getMatchType());
holder.matchStatusTv.setText(model.getMatchStatus());
holder.dateTv.setText(model.getDate());
//handle item clicks
holder.cardView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//will do later
}
});
}
@Override
public int getItemCount() {
return modelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
//define view objects
TextView team1Tv, team2Tv, matchTypeTv, matchStatusTv, dateTv;
CardView cardView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// intialize the view objects
team1Tv = itemView.findViewById(R.id.team1TV);
team2Tv = itemView.findViewById(R.id.team2TV);
matchTypeTv = itemView.findViewById(R.id.matchTypeTV);
matchStatusTv = itemView.findViewById(R.id.matchStatusTV);
dateTv = itemView.findViewById(R.id.dateTV);
cardView = itemView.findViewById(R.id.cardView);
}
}}
onBindViewHolder()出现异常:team1Tv=itemView。findViewById(R.id.team1TV);
**Row.xml**
这是为显示磁贴列表而创建的自定义xml。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="2">
<TableRow>
<TextView
android:text="Team1"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/team1TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Team2"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/team2TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Match Type"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/matchTypeTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Match Status"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/matchStatusTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Date"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"/>
<TextView
android:id="@+id/dateTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</androidx.cardview.widget.CardView>
----------------
**activity_main.xml**
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
**Error logs from LogCat:**
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sportsx, PID: 20974
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.sportsx.MyAdapter.onBindViewHolder(MyAdapter.java:53)
at com.example.sportsx.MyAdapter.onBindViewHolder(MyAdapter.java:18)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:1041)
at android.view.View.layout(View.java:22413)
at android.view.ViewGroup.layout(ViewGroup.java:6584)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3382)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2849)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1897)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8516)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:986)
at android.view.Choreographer.doCallbacks(Choreographer.java:764)
at android.view.Choreographer.doFrame(Choreographer.java:699)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:965)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 20974 SIG: 9
Process 20974 terminated.
它在加载时崩溃了,所以任何人都猜到出了什么问题。提前谢谢。
您在onCreateViewHolder方法中为RecyclerView项指定了不正确的布局。
因此,将视图通货膨胀替换为:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
(使用R.layout.row,而不是R.layout.activity\u main)
当我想要启动我的应用程序时,我收到错误“尝试调用空对象引用上的虚拟方法android.widget.TextView.setText(java.lang.CharSequence)”。 错误输出 主要活动。Java语言 请帮助。
我正在尝试创建一个回收视图,在instagram中以故事的形式显示一张图片。。我已经完成了所有工作,但代码仍然没有运行,并在com上告诉我一个错误。实例回收视图。适配器。com上的onBindViewHolder(Adapter.java:36)。实例回收视图。适配器。onBindViewHolder(Adapter.java:14) 公共类适配器扩展了RecyclerView。适配器{ }
我是android新手。最近,我尝试通过活动在两个片段之间进行通信。基本上在这里,我将按钮的位置从一个片段发送到另一个由textview组成的片段。但我得到了一个错误:尝试调用虚拟方法void-android。小装置。文本视图。setText(java.lang.CharSequence)' 我尝试了值id是否被传递。我在Log上打印了该值并成功打印。我已经搜索了很多这个错误并尝试了替代方案;使用
这个我的错误报告是尝试调用虚拟方法android.widget.TextView.setText(java.lang.CharSequence)'null对象 (私有void setTotalAmount(String totalitem,String itemprice,String anterharga,String totalasemuanya))
问题内容: 我正在尝试获取一些信息,但我的应用程序崩溃并显示以下消息: 这是代码: 这是一个纯课。的哪个调用不包含this 。我做错了吗?应始终位于内吗? 根据要求整体: 问题答案: 改成 您为布局充气。视图属于膨胀的布局。因此,使用view对象在中初始化视图 片段由活动主持 您可以在视图中使用和初始化 还在中初始化TextView 。由于Asynctask是一个内部类,因此您可以在那里更新ui
当我运行我的应用程序时,我有一个异常显示,所以我想帮助我:)异常是:java。lang.NullPointerException:尝试调用虚拟方法“int java”。lang.String。空对象引用上的长度() 当我单击Login类中的dd1按钮和按钮实现(public void getData(View v))方法时,会显示异常 非常感谢。 登录类 HttpManager类 日志: