简单自定义ListView应用程序。每次都会崩溃

时间:2016-03-16 15:13:54

标签: android

我是Android的新手,所以如果您遇到的错误是一个大错,我真的很抱歉。我尝试使用custom_row.xml作为布局文件并使用CustomAdapter.java作为自定义适配器来制作自定义ListView(每行中的ImageView和TextView)。

我不明白为什么这个应用程序每次运行时都会崩溃。异常(如Android监视器中所示)是由CustomAdapter.java文件中的此行引起的。

View customView = myInflater.inflate(R.layout.custom_row, parent, false);

我附上了所有Java和Layout代码供参考。感谢。

CustomAdapter.java

package com.abhinavankur.listexample;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;

class CustomAdapter extends ArrayAdapter<String> {

CustomAdapter(Context context, String[] teams) {
    super(context,R.layout.custom_row,teams);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater myInflater = LayoutInflater.from(getContext());
    View customView = myInflater.inflate(R.layout.custom_row, parent, false);

    String singleTeam = getItem(position);
    TextView myText = (TextView)  customView.findViewById(R.id.myText);
    ImageView myImage= (ImageView) customView.findViewById(R.id.myImage);

    myText.setText(singleTeam);
    myImage.setImageResource(R.drawable.mine);
    return customView;
}
}

custom_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="80dp"
    android:layout_height="80dp"
    android:id="@+id/myImage"
    android:src="@drawable/mine"
    android:layout_margin="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/myText"
    android:layout_margin="5dp" />
</LinearLayout>

activity_main.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="com.abhinavankur.listexample.MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myListView">
</ListView>
</RelativeLayout>

MainActivity.java

package com.abhinavankur.listexample;

import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

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


String teams[] = {"Real Madrid","Manchester United","Barcelona","Chelsea","Bayern Munich","PSG","Borussia Dortmund","Liverpool","Arsenal","Valencia","Villareal","Leicester City","AS Roma"};

    ListAdapter myAdapter = new CustomAdapter(this, teams);
    ListView myList = (ListView) findViewById(R.id.myListView);
    myList.setAdapter(myAdapter);
    Toast.makeText(this,"List Shown",Toast.LENGTH_LONG).show();

    myList.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String team = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(MainActivity.this,team,Toast.LENGTH_SHORT).show();
                }
            }
    );
}

}

1 个答案:

答案 0 :(得分:0)

如下所示更改CustomAdapter。你是以错误的方式给布局充气。

class CustomAdapter extends ArrayAdapter<String> {

    Context context;

    CustomAdapter(Context context, String[] teams) {
        super(context,R.layout.custom_row,teams);
        this.context = context
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.custom_row, null);
        }

        ....
        ....

}