开发者

Custom ListView adapter Force close :(

开发者 https://www.devze.com 2023-01-23 23:46 出处:网络
I am a newbe to android and have been following the tutorials posted online to experiment with coding for android. I am trying to display a list which has input from two different arrays, but every ti

I am a newbe to android and have been following the tutorials posted online to experiment with coding for android. I am trying to display a list which has input from two different arrays, but every time i run the code i keep on getting a force close and can seem to figure out what i am doing wrong. here is my code ( this is from an online tutorial)

Main.XML

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

  <TextView android:id="@+id/TextView01" 
          android:layout_height="wrap_content" 
          android:text="List of Country &amp; their denotation" 
          android:textStyle="normal|bold" 
          android:gravity="center_vertical|center_horizontal"
          android:layout_width="fill_parent"> 
  </TextView>
  <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
            android:layout_width="fill_parent">
  </ListView>
</LinearLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="wrap_content" 
              android:gravity="left|center"
              android:layout_width="wrap_content" 
              android:paddingBottom="5px"
              android:paddingTop="5px" 
              android:paddingLeft="5px">

  <TextView android:id="@+id/TextView01" 
            android:layout_width="wrap_content"
            android:layout_he开发者_如何学编程ight="wrap_content" 
            android:gravity="center"
            android:background="@drawable/bg" 
            android:textColor="#FFFF00"
            android:text="hi"/>

  <TextView android:text="@+id/TextView02" 
            android:id="@+id/TextView02"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginLeft="10px" 
            android:textColor="#0099CC"/>

</LinearLayout> 

display.java

package com.example.display

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class customlistview extends Activity {

  private static class EfficientAdapter extends BaseAdapter {
  private LayoutInflater mInflater;

  public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
  }

  public int getCount() {
    return country.length;
  }

  public Object getItem(int position) {
    return position;
  }

  public long getItemId(int position) {
    return position;
  }

  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.listview, null);
      holder = new ViewHolder();
      holder.text = (TextView) convertView.findViewById(R.id.TextView01);
      holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);

      convertView.setTag(holder);
      } else {
        holder = (ViewHolder) convertView.getTag();
      }

      holder.text.setText(curr[position]);
      holder.text2.setText(country[position]);

      return convertView;
    }

    static class ViewHolder {
      TextView text;
      TextView text2;
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView l1 = (ListView) findViewById(R.id.ListView01);
    l1.setAdapter(new EfficientAdapter(this));
  }

  private static final String[] country = { "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia", "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania", "Luxembourg" };

  private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD", "EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD", "LTL ", "EUR"};

}

This code can also be found at http://www.androidpeople.com/android-custom-listview-tutorial-example/,

I have tried several times, changing different values and even trying to print one array but have been unsuccessful at it and keep on getting a force close. I would appreciate it if someone would be able to point out what is wrong with the code and point me in the right direction.


Did you include your activity file inside AndroidManifest.xml file. Every Activity Needs to be included in this xml file. Syntax:

Also check your launcher activity is set in xml file

0

精彩评论

暂无评论...
验证码 换一张
取 消