当前位置: 首页 > 知识库问答 >
问题:

应用程序从listview实现崩溃

祁博涛
2023-03-14

我最近尝试为ListView创建一个自定义行。我试图从教程中完成它,但当我试图更改它时,它崩溃了,以便行与我希望我的应用程序显示的内容一起工作。错误读出在底部。

public class HomeActivityListview extends AppCompatActivity {

    ListView lv;
    Context context;
    ArrayList dogName;
    ArrayList dogID;
    public static int [] dogImages={R.id.dogimage};
    public static String [] dogNames={};

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

        context=this;

        lv=(ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(this, dogImages));
    }


    public void sendProfile(View view) {
        Intent intent = new Intent(getApplicationContext(),  DogProfileActivity.class);
        startActivity(intent);

    }

    public void NumberOne(View view) {
        AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
        alertDlg.setMessage("Confirm Dog went number one");
        alertDlg.setCancelable(false);

        alertDlg.setPositiveButton("Confirm", new     DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //HomeActivity.super.onBackPressed();
            }
        });

        alertDlg.setNegativeButton("Change", new     DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //HomeActivity.super.onBackPressed();
            }
        });
        alertDlg.create().show();

    }
    public void NumberTwo(View view) {
        AlertDialog.Builder alertDlg = new AlertDialog.Builder(this);
        alertDlg.setMessage("Confirm Dog went number two");
        alertDlg.setCancelable(false);

        alertDlg.setPositiveButton("Confirm", new     DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //HomeActivity.super.onBackPressed();
            }
        });

        alertDlg.setNegativeButton("Change", new     DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //HomeActivity.super.onBackPressed();
            }
        });
        alertDlg.create().show();

    }

    public void AddNewDog(View view) {
        Intent intent = new Intent(getApplicationContext(),     NewDogActivity.class);
        startActivity(intent);
    }

}
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(HomeActivityListview homeActivityListview, int[]     dogImages) {
    // TODO Auto-generated constructor stub
    context=homeActivityListview;
    imageId=dogImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imageId.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{

    ImageView img;
    ImageView img1;
    ImageView img2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)             {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.home_single_row, null);
    holder.img=(ImageView) rowView.findViewById(R.id.dogimage);
    holder.img1=(ImageView) rowView.findViewById(R.id.dog1num1);
    holder.img2=(ImageView) rowView.findViewById(R.id.dog1num2);
    holder.img.setImageResource(imageId[position]);
    holder.img1.setImageResource(imageId[position]);
    holder.img2.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position],     Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
    android:layout_width="150dip"
    android:layout_height="150dip"
    android:id="@+id/dogimage"
    android:clickable="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@drawable/dogpic"
    android:onClick="sendProfile"/>

<ImageButton
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:id="@+id/dog1num1"
    android:background="@drawable/num1"
    android:clickable="true"
    android:onClick="NumberOne"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="75dp" />

<ImageButton
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:id="@+id/dog1num2"
    android:background="@drawable/num2"
    android:clickable="true"
    android:onClick="NumberTwo"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="75dp" />

--------homescreen.xml---------------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cs495capstone.edu.bsu.doggydid.HomeActivityListview
"
android:background="@drawable/screenbackground">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/selectprofiletext"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#2c7134"
    android:text="Select a profile or make a post:"
    />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_above="@+id/newdogbutton"
    android:entries="@array/dogs"
    android:layout_below="@+id/selectprofiletext"
    android:layout_marginTop="10dip" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add new dog"
    android:id="@+id/newdogbutton"
    android:background="#2c7134"
    android:allowUndo="true"
    android:textColor="#f3ca83"
    android:clickable="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="AddNewDog"
    />
</RelativeLayout>

----------读出错误---------------------------------------------

04-07 14:06:40.998 253 2-2532/cs495capstone.edu.bsu.doggydid e/androidruntime:致命异常:main process:cs495capstone.edu.bsu.doggydid,pid:2532 android.content.ress.resources$NOTFoundException:Resource“cs495capstone.edu.bsu.doggydid:id/dogimage”(7f0d008e)不是可绘图(颜色或路径):TypedValue{:2390)在Android.content.res.resources.LoadDrawable(resources.java:2330)在Android.content.res.resources.getDrawable(resources.java:758)在Android.content.context.getDrawable(context.java:402)在Android.support.v4.content.contextCompatapi21.getDrawable(contextCompatapi21.java:26)在Android.support.v4.content.contextCompat.java:321)在widget.tintManager.getDrawable(TintManager.java:168)在Android.support.v7.widget.appcompatimageHelper.SetimageResource(AppCompaTimageHelper.java:51)在Android.support.v7.widget.appcompatimageButton.setimageResource(appcompatimageButton.java:73)在cs495capstone.edu.bsu.doggydid.customAdapter.getview(customAdapter.java:57)在Android.widget.abslistview.getview(customAdapter.java:57)在Android.widget.abslistview.getview(在Android.widget.relativelayout.java:727)在Android.widget.relativelayout.onMeasure(relativelayout.java:463),在Android.view.viewgroup.measure(view.java:17430)在Android.view.viewgroup.measure(view.java:17430),在Android.widget.framelayout.onMeasure(view.group.v7.widget.contentframelayout.onMeasure(id.support.v7.widget.acOnMeasure(actionbaroverlaylayout.java:391)在Android.view.view.viewgroup.measure(view.java:17430)在Android.view.viewgroup.measure(viewgroup.java:17430)在Android.widget.frameLayout.onMeasure(viewgroup.java:5463),在Android.view.viewgroup.measure(viewgroup.java:430)在Android.view.viewgroup.measure(722)在Android.widget.linearlayout.onMeasure(linearlayout.java:613)在Android.view.viewgroup.measure(view.java:17430)在Android.view.viewgroup.measuryChildWith Margins(viewgroup.java:17430)在Android.widget.framelayout.onMeasure(framelayout.java:5463),android.widget.framelayout.onMeasure(AR在Android.view.viewrootimpl.java:1166)在Android.view.viewrootimpl.performtraversals(viewrootimpl.java:1372),在Android.view.viewrootimpl.doTraversal(viewrootimpl.java:1054),在Android.view.viewrootimpl.doTraversal(viewrootimpl.java:1054),在Android.view.viewrootimpl.java:5779),在Android.view.viewrootimpl.run(viewrootimpl.java:5779),.view.choreographer$frameDisplayEventReceiver.run(choreographer.java:753),android.os.handler.handleCallback(handler.java:739),android.os.handler.dispatchMessage(handler.java:95),android.os.handler.dispatchMessage(handler.java:135),android.app.activityThread.main(activityThread.java:5221)goteinit.java:899)位于com.android.internal.os.zygoteinit.main(zygoteinit.java:694)04-07 14:07:23.942 2532-2532/cs495capstone.edu.bsu.doggydid I/process:发送信号。PID:2532签名:9

共有1个答案

子车雅珺
2023-03-14

你必须通过一个抽屉-

holder.img.setImageResource(R.drawable.dogpic);
holder.img1.setImageResource(R.drawable.dogpic);
holder.img2.setImageResource(R.drawable.dogpic);
public static int [] dogImages={R.id.dogimage};

您正在传递Id,您应该传递drawable。

 类似资料:
  • 当我从列表视图小部件或应用程序中注释一行时,它可以工作,但当我取消注释时,它根本不出现... 它是一个listview来呈现您通过api收到的通知列表,api与未来的构建器一起正常工作,但是当我添加listBuilder时,它停止工作…… 代码 日志终端

  • 问题内容: 当我尝试访问Flask应用程序时,该应用程序崩溃了。 这类似于this或this。但是,我的设置似乎正确。 flask.cli.NoAppException:提供的文件/路径(服务器)似乎不存在。请确认路径正确。如果应用不在PYTHONPATH上,请确保扩展名为.py 我的环境变量设置正确。 我的服务器文件是y目录中没有任何文件。 我不记得对代码进行任何特殊更改。该错误可能来自哪里?

  • 在以前的代码上,谁医生帮了我 现在,android应用程序在我的手机上运行时崩溃了,这是错误日志 第一个问题是在我用相机扫描二维码后,它不能显示在二维码的结果进入 第二个问题是,我从存储器中选择了一个QRcode图像,然后点击确认,它崩溃了 下面是我认为的问题 类型不匹配:推断的类型是Uri?但乌里是意料之中的 冗余SAM构造函数 'onRequestPermissionsResult(Int,数

  • 我有以下代码,试图在ListView中创建一个简单的ListAdapter(我以前使用过这段代码,这是我唯一更改内容的地方): 当单步执行时,什么都不会发生,但当运行时,我在ChoreoGrapher中得到了一个null点异常。doCallbacks,当我创建一个空的数组列表时,它不会崩溃: 什么是编舞,为什么它会使我的应用程序崩溃? 我完全被困在可能的问题上,或者如何找出问题所在。Eclipse

  • 我刚刚在Google Play上构建并部署了一个应用程序。当我通过Android Studio运行它时,它运行得很好,但现在当我从Google Play下载它时,它崩溃了。因为这是我第一次,我甚至不知道如何查看从Google Play下载的应用程序的崩溃报告/堆栈跟踪。我感谢所有的帮助。 更新 所以我得到了APK的堆栈跟踪。它告诉我我的类无法创建我的类。我不知道为什么它会出现这个错误。一切正常。看