应用程序从listview实现崩溃

时间:2016-04-07 18:02:03

标签: java android xml android-layout listview

我最近尝试为listview创建自定义行。我尝试从教程中做到这一点,但是当我尝试更改它时它会崩溃,因此行与我希望我的应用程序显示的内容一起工作。错误读数位于底部。

也与问题无关但我很好奇,如果我能够在自定义适配器类中使用imageId作为我想要在行中的3个图像中的每个图像,或者它们是否需要是唯一的。<登记/>     holder.img2.setImageResource(图像标识[位置]);

----- Homeactivitylistview.java -----------

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);
    }

}

------------------- customadapter.java -------------------------

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;
}

}

---------------------- singlerow.xml ---------------------- ----

<?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 2532-2532 / cs495capstone.edu.bsu.doggydid   E / AndroidRuntime:致命异常:主要                                                                                 处理:cs495capstone.edu.bsu.doggydid,PID:2532                                                                                 android.content.res.Resources $ NotFoundException:资源   &#34; cs495capstone.edu.bsu.doggydid:ID / dogimage&#34; (7f0d008e)不是   可绘制(颜色或路径):TypedValue {t = 0x12 / d = 0x0 a = 2 r = 0x7f0d008e}                                                                                     在   android.content.res.Resources.loadDrawableForCookie(Resources.java: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.getDrawable(ContextCompat.java:321)                                                                                     在   android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)                                                                                     在   android.support.v7.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.obtainView(AbsListView.java:2344)                                                                                     在android.widget.ListView.measureHeightOfChildren(ListView.java:1270)                                                                                     在android.widget.ListView.onMeasure(ListView.java:1182)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在   android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:727)                                                                                     在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                                                     在   android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)                                                                                     在   android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)                                                                                     在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)                                                                                     在android.widget.LinearLayout.measureVertical(LinearLayout.java:722)                                                                                     在android.widget.LinearLayout.onMeasure(LinearLayout.java:613)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)                                                                                     在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)                                                                                     在   com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2560)                                                                                     在android.view.View.measure(View.java:17430)                                                                                     在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)                                                                                     在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)                                                                                     在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)                                                                                     在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)                                                                                     在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)                                                                                     在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)                                                                                     在android.view.Choreographer.doCallbacks(Choreographer.java:580)                                                                                     在android.view.Choreographer.doFrame(Choreographer.java:550)                                                                                     在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)                                                                                     在android.os.Handler.handleCallback(Handler.java:739)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                     在android.os.Looper.loop(Looper.java:135)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5221)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在java.lang.reflect.Method.invoke(Method.java:372)                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.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 SIG:9

1 个答案:

答案 0 :(得分:1)

你必须传递一个drawable -

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,你应该传递d​​rawable。