当按下 menuItem 以在 android 中显示用于列表视图的自定义适配器时,应用程序崩溃
Posted
技术标签:
【中文标题】当按下 menuItem 以在 android 中显示用于列表视图的自定义适配器时,应用程序崩溃【英文标题】:App crashes when menuItem is pressed to display Custom Adapter for listview in android 【发布时间】:2020-09-07 09:29:13 【问题描述】:每当按下 menuItem 时,我都会尝试显示一个列表视图。我想显示自定义布局,因为我需要用于无法正常工作且我的应用程序崩溃的列表视图的自定义适配器。我的 android 应用程序中有 3-4 个自定义适配器工作正常,但是自从我添加了另一个......前一个也停止工作我不知道为什么!我还在许多论坛上看到了几乎所有与此相关的问题,但对我没有任何帮助。请帮助我哪里错了..
下面是我的 menuItems 代码..
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
switch (menuItem.getItemId())
case R.id.settings:
Intent intent = new Intent(this, OpenSettings.class);
startActivity(intent);
break;
case R.id.debacha:
Intent intent1 = new Intent(AyahsDisplay.this,DebachaDisplay.class);
this.startActivity(intent1);
break;
return super.onOptionsItemSelected(menuItem);
当按下 id 为“debach”的菜单项时,我将开始新的活动,我打算在其中显示自定义列表视图,其中出现错误和应用程序崩溃..
该部分的代码如下...
public class DebachaDisplay extends AppCompatActivity
ListView list1;
ArrayList<debacha> debachas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_debacha_display);
try
parseXML(); //here i am parsing XML file to load data in "debachas" which is working fine
catch (XmlPullParserException e)
catch (IOException e)
private void printDebacha(ArrayList<debacha> debachas)
list1 = findViewById(R.id.debachaList);
CustomDebachaview customDebachaview = new CustomDebachaview(DebachaDisplay.this,debachas);
list1.setAdapter(customDebachaview);
下面是 DisplayDebcha 活动的布局文件
<?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_
android:layout_
tools:context=".DebachaDisplay">
<ListView
android:id="@+id/debachaList"
android:background="#FDFFE9"
android:layout_
android:layout_
>
</ListView>
</LinearLayout>
下面是我的自定义适配器类的代码...
package com.example.demo;
import android.app.Activity;
import android.content.Intent;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class CustomDebachaview extends ArrayAdapter<debacha>
private ArrayList<debacha> debachas;
private Activity context;
public CustomDebachaview(Activity context,ArrayList<debacha> debachas)
super(context,R.layout.debacha_list_view,debachas);
this.debachas = debachas;
this.context = context;
@NonNull
@Override
public View getView(final int position , @Nullable View convertView , @Nullable final ViewGroup parent)
View r = convertView;
CustomDebachaview.viewHolder viewHolder = null;
if(r == null)
LayoutInflater layoutInflater = context.getLayoutInflater();
r = layoutInflater.inflate(R.layout.debacha_list_view,null,true);
viewHolder = new CustomDebachaview.viewHolder(r);
r.setTag(viewHolder);
else
viewHolder = (CustomDebachaview.viewHolder) r.getTag();
viewHolder.Dnumber.setText("﴾ " + debachas.get(position).getDNumber() + " ﴿");
viewHolder.Dname.setText(debachas.get(position).getDName());
return r;
class viewHolder
TextView Dnumber;
TextView Dname;
viewHolder(View v)
Dname = v.findViewById(R.id.Dnumber);
Dname = v.findViewById(R.id.Dname);
下面是自定义适配器列表视图布局文件代码...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_>
<TextView
android:id="@+id/Dnumber"
android:layout_
android:layout_
android:text="debacha number"
android:paddingLeft="5dp"
android:textSize="17dp"
android:textColor="#297B16"
android:textStyle="bold"
/>
<TextView
android:id="@+id/Dname"
android:layout_
android:layout_
android:text="Dname"
android:textSize="25dp"
android:textColor="#226A12"
/>
</LinearLayout>
我收到以下错误..
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo, PID: 1040
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.demo.CustomDebachaview.getView(CustomDebachaview.java:42)
at android.widget.AbsListView.obtainView(AbsListView.java:3219)
at android.widget.ListView.makeAndAddView(ListView.java:2219)
at android.widget.ListView.fillDown(ListView.java:827)
at android.widget.ListView.fillFromTop(ListView.java:888)
at android.widget.ListView.layoutChildren(ListView.java:1958)
at android.widget.AbsListView.onLayout(AbsListView.java:2991)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1801)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1567)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
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:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
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:1075)
at android.view.View.layout(View.java:22407)
at android.view.ViewGroup.layout(ViewGroup.java:6589)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3371)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2840)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8503)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
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:7073)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
I/Process: Sending signal. PID: 1040 SIG: 9
Process 1040 terminated.
非常感谢任何帮助:)
【问题讨论】:
这能回答你的问题吗? Unfortunately MyApp has stopped. How can I solve this? 你不应该有一个 try catch 语句并且不记录或至少处理异常:try parseXML(); //here i am parsing XML file to load data in "debachas" which is working fine catch (XmlPullParserException e) catch (IOException e)
虽然这与你的问题无关,但也请发布你的错误日志
@a_local_nobody 我已经用错误日志编辑了我的问题..你能看看并告诉可能是什么问题吗?
我认为我在 CustomAdapter 类的 getView 方法中的 viewHolder 对象可能是 Null...我不知道为什么会这样
【参考方案1】:
我的错...这只是一个愚蠢的错误,viewholder 构造函数中的错字导致 Dnumber 为空:(
class viewHolder
TextView Dnumber;
TextView Dname;
viewHolder(View v)
Dname = v.findViewById(R.id.Dnumber);
Dname = v.findViewById(R.id.Dname);
我正在初始化 2 次 Dname。
经验教训:请检查您的打字错误:)
【讨论】:
以上是关于当按下 menuItem 以在 android 中显示用于列表视图的自定义适配器时,应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
当按下 Flutter 中编程的按钮时,有没有办法可以运行 python 脚本?
UIMenuController:如何判断哪个 menuItem 被按下?