Android Studio - 自定义ListView崩溃

时间:2017-05-13 22:42:01

标签: java android xml listview

所以,最近关注了有关如何制作自定义ListView的视频说明:https://www.youtube.com/watch?v=D0or0X12FMM

该程序在启动时崩溃,其DOA并且在崩溃之前显示没有任何内容。

It's not logging any errors to the console.

这是我在video + mainifest中显示的所有部分的代码:

我觉得这是清单的问题。仅基于其他帖子。提前谢谢!

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.doubl.my_application">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ListTest">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_list_test.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:padding="16dp"
    tools:context="com.example.doubl.my_application.ListTest">

    <ListView
        android:id="@+id/list1"
        android:layout_width="312dp"
        android:layout_height="519dp"
        android:layout_alignParentTop="true"/>
</android.support.constraint.ConstraintLayout>

row.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">

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp"
        android:id="@+id/icon"
        android:src="@mipmap/ic_launcher"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:textColor="#33CC33"
            android:id="@+id/text1"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"
            android:text="Medium Text"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text2"
            android:layout_marginLeft="10dp"
            android:text="TextView"
            />

    </LinearLayout>

</LinearLayout>

ListTest.java

package com.example.doubl.my_application;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class ListTest extends AppCompatActivity {

    ListView list;
    String [] titles;
    String [] description;
    int[] imgs = {R.drawable.facebook, R.drawable.instagram, R.drawable.twitter, R.drawable.google};

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


        Resources res = getResources();
        titles = res.getStringArray(R.array.titles);
        description = res.getStringArray(R.array.description);

        list = (ListView) findViewById(R.id.list1);
        MyAdapter adapter = new MyAdapter(this, titles, imgs, description);
        list.setAdapter(adapter);
    }

    class MyAdapter extends ArrayAdapter<String>{

        Context context;
        int[] imgss;
        String myTitles[];
        String myDescription[];

        MyAdapter(Context c, String []titles, int[] img, String[]description){
            super(c, R.layout.row, R.id.text1, titles);
            this.context = c;
            this.imgss = img;
            this.myDescription = description;
            this.myTitles = titles;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.row,parent,false);
            ImageView images = (ImageView) findViewById(R.id.icon);
            TextView myTitle = (TextView) findViewById(R.id.text1);
            TextView myDescription = (TextView) findViewById(R.id.text2);
            images.setImageResource(R.drawable.facebook);
            images.setImageResource(imgs[position]);
            myTitle.setText(titles[position]);
            myDescription.setText(description[position]);
            return row;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

Just Goto:

Android Studio --> File --> Setting --> Build, execution, deploy --> Instant run

并禁用即时运行。