Android运行时错误:应用程序突然停止

时间:2016-11-01 03:43:16

标签: android listview

我的应用是执行ListView。当它运行时,错误消息通知“不幸的是,应用程序已停止”。这是我的代码。

    package com.example.admin.customadapter;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ListView;

    import java.util.ArrayList;


    public class MainActivity extends AppCompatActivity {
      ListView listView;
      ArrayList<Nation>nationifo;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      listView = (ListView)findViewById(R.id.ListViewNation);
      nationifo = new ArrayList<>();
      nationifo.add(new Nation("VietNam",1945));
      nationifo.add(new Nation("Malaysia",1975));
      nationifo.add(new Nation("Laos",1943));

         NationAdapter adp = new NationAdapter(
                MainActivity.this,
                R.layout.nation_list,
                nationifo
        );
        listView.setAdapter(adp);
    }
  }

Nation.java

   package com.example.admin.customadapter;
   public class Nation {
   public String name;
   public Integer year;
   public Nation(String Name, Integer Year){
       Name = name;
       Year = year;
   }
 }

NationAdapter.java

package com.example.admin.customadapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;
public class NationAdapter extends BaseAdapter {
    Context mycontext;
    int mylayout;
    List<Nation>arrayNation;
     public NationAdapter (Context context, int layout,    List<Nation>nationList){
        mycontext = context;
        mylayout = layout;
        arrayNation = nationList;
    }
      public int getCount() {
         return arrayNation.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)     mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(mylayout,null);

        TextView txtv1 = (TextView) convertView.findViewById(R.id.textView);
        txtv1.setText(arrayNation.get(position).name);
        TextView txtv2 =(TextView) convertView.findViewById(R.id.textView2);
        txtv2.setText(String.valueOf(arrayNation.get(position).year));
        return convertView;
    }
}

nation_list.xml

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

    <TextView
       android:textColor="#ff0400"
       android:text="TextView"
       android:layout_width="match_parent"
       android:layout_height="30dp"
       android:id="@+id/textView" />

    <TextView
       android:textColor="#0022ff"
       android:text="TextView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/textView2" />
 </LinearLayout>

我的Android Studio是2.2。但我不认为这个版本与另一个版本不同。

1 个答案:

答案 0 :(得分:0)

尝试使用以下

convertView = inflater.inflate(mylayout,parent,false);

而不是

convertView = inflater.inflate(mylayout,null);