0

我正在尝试在android的每个条目中实现一个带有图像和文本的列表视图

但是我收到一条消息:“您的应用程序必须停止...”

这是我的主要活动:

package com.example.temp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity
{

 EditText edittext;
 ListView listview;

 String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
   "Eight", "Nine", "Ten" };

 int[] image = { R.drawable.logo, R.drawable.logo, R.drawable.logo,
   R.drawable.logo, R.drawable.logo, R.drawable.logo, R.drawable.logo,
   R.drawable.logo, R.drawable.logo, R.drawable.logo };

 int textlength = 0;

 ArrayList<String> text_sort = new ArrayList<String>();
 ArrayList<Integer> image_sort = new ArrayList<Integer>();

 public void onCreate(Bundle savedInstanceState)
 {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  listview = (ListView) findViewById(R.id.ListView01);
  listview.setAdapter(new MyCustomAdapter(text, image));

  edittext.addTextChangedListener(new TextWatcher()
  {

   public void afterTextChanged(Editable s)
   {

   }

   public void beforeTextChanged(CharSequence s, int start,
    int count, int after)
   {

   }

   public void onTextChanged(CharSequence s, int start,
    int before, int count)
   {

    textlength = edittext.getText().length();
    text_sort.clear();
    image_sort.clear();

    for (int i = 0; i < text.length; i++)
    {
     if (textlength <= text[i].length())
     {
      if (edittext.getText().toString().
   equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
      {
       text_sort.add(text[i]);
       image_sort.add(image[i]);
      }
     }
    }

    listview.setAdapter(new MyCustomAdapter
     (text_sort, image_sort));

   }
  });
 }

 class MyCustomAdapter extends BaseAdapter
 {

  String[] data_text;
  int[] data_image;

  MyCustomAdapter()
  {

  }

  MyCustomAdapter(String[] text, int[] image)
  {
   data_text = text;
   data_image = image;
  }

  MyCustomAdapter(ArrayList<String> text, ArrayList<Integer> image)
  { 

   data_text = new String[text.size()];
   data_image = new int[image.size()];

   for(int i=0;i<text.size();i++)
   {
    data_text[i] = text.get(i);
    data_image[i] = image.get(i);
   }

  }

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

  public String getItem(int position)
  {
   return null;
  }

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

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

   LayoutInflater inflater = getLayoutInflater();
   View row;

   row = inflater.inflate(R.layout.listview, parent, false);

   TextView textview = (TextView) row.findViewById(R.id.TextView01);
   ImageView imageview = (ImageView) row
     .findViewById(R.id.ImageView01);

   textview.setText(data_text[position]);
   imageview.setImageResource(data_image[position]);

   return (row);

  }
 }

}

这是我的 listview.xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ImageView01"
            android:layout_centerHorizontal="true"
            android:singleLine="true"
            android:text="Israel News"
            android:textSize="18sp"
            android:textStyle="bold" />

    </RelativeLayout>

这里是我的activity_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">

                <EditText android:id="@+id/EditText01"
                             android:layout_height="wrap_content"
                                android:layout_width="fill_parent"
                                  android:hint="Search">                               
                </EditText>

                <ListView android:id="@+id/ListView01"
                                android:layout_height="wrap_content"
                                android:layout_width="fill_parent">
                 </ListView>

</LinearLayout>

这是logcat:

03-03 11:47:02.053: E/AndroidRuntime(770): FATAL EXCEPTION: main
03-03 11:47:02.053: E/AndroidRuntime(770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.temp/com.example.temp.MainActivity}: java.lang.NullPointerException
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.os.Looper.loop(Looper.java:137)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-03 11:47:02.053: E/AndroidRuntime(770):  at java.lang.reflect.Method.invokeNative(Native Method)
03-03 11:47:02.053: E/AndroidRuntime(770):  at java.lang.reflect.Method.invoke(Method.java:511)
03-03 11:47:02.053: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-03 11:47:02.053: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-03 11:47:02.053: E/AndroidRuntime(770):  at dalvik.system.NativeStart.main(Native Method)
03-03 11:47:02.053: E/AndroidRuntime(770): Caused by: java.lang.NullPointerException
03-03 11:47:02.053: E/AndroidRuntime(770):  at com.example.temp.MainActivity.onCreate(MainActivity.java:45)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.Activity.performCreate(Activity.java:5104)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-03 11:47:02.053: E/AndroidRuntime(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-03 11:47:02.053: E/AndroidRuntime(770):  ... 11 more

我真的不知道是什么问题。。

谢谢

4

5 回答 5

2

您的 edittext 为空,因为您没有得到 Edit 文本,

像这样获取 EditText 在将 TextChangedListener 添加到您的edittext之前添加此行

editext=(EditText)findViewById(R.id.EditText01);
于 2013-03-03T12:05:16.937 回答
2

您在 MainActivity 的第 45 行使用未初始化的属性 edittext。在第 45 行之前添加此行:

edittext = (EditText) findViewById(R.id.EditText01);

性能提示:检查 ViewHolder 模式以使您的列表滚动更顺畅:http ://www.jmanzano.es/blog/?p=166

于 2013-03-03T12:05:53.370 回答
0

您未初始化的编辑文本。默认情况下,在 java 中,如果您声明任何变量,并且如果您没有初始化任何东西,它将具有一些预定义的值。默认情况下,在 java 中,如果您不分配任何对象,它将具有空值。如果你想使用该对象来调用函数/访问它的数据成员,它会抛出 NULLPOINTER EXCEPTION。

于 2013-03-03T12:20:24.980 回答
0

您已将 edittext 声明为 EditText 但尚未对其进行初始化。当您尝试对其执行 addWatcher 时,您实际上是在 null 上执行它。试试这个:

edittext = new EditText(this);

或者

edittext = (EditText) findByViewId(R.id.EditText01);
于 2013-03-03T12:08:27.730 回答
-2

你得到一个空点异常。尝试调试它并检查第 45 行或空指针的位置。

于 2013-03-03T12:02:39.523 回答