源码的目的是点击按钮后,将EditText的内容加到AutoCompleteTextView的List内
把红字一行去掉,直到Toast.makeText都没问题;
加上一行红字,就挂了……
问一下这行有什么问题?谢谢!import java.util.List;
import android.app.Activity;
import android.widget.*;
import android.os.Bundle;
import android.view.*;public class AnTestActivity extends Activity {
private List<String> l;
private EditText et;
private Button bt;
private AutoCompleteTextView actv;
private ArrayAdapter<String> aa;

class Bocl implements Button.OnClickListener{

public void onClick(View v){
Toast.makeText(AnTestActivity.this, et.getText(), Toast.LENGTH_SHORT).show();
l.add(et.getText().toString());
}
}

public void onCreate(Bundle bnd){
super.onCreate(bnd);
}
public void onStart(){
super.onStart();
setContentView(R.layout.beforeclick);
aa=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, l);
et=(EditText)findViewById(R.id.et);
bt=(Button)findViewById(R.id.bt);
bt.setOnClickListener(new Bocl());
actv=(AutoCompleteTextView)findViewById(R.id.actv);
actv.setAdapter(aa);
}
}
beforeclick.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Input Below"
    ></TextView>
<EditText
    android:id="@+id/et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</EditText> <Button
    android:id="@+id/bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" /> <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Input Next Time"
    ></TextView>
    <AutoCompleteTextView
        android:id="@+id/actv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </AutoCompleteTextView></LinearLayout>

解决方案 »

  1.   

    应该是因为你的List l没有初始化,所以l=null,使List<String> l = new ArrayList<String>()试试
      

  2.   

    再请教一下可以吗??
    我定义l的时候已经强制l为List<String>
    我用l.add的时候add的内容也toString()了
    为什么还一定要初始化变量啊??
    非常感谢!
      

  3.   

    学一下JAVA基础吧,任何一个对象,没有实例化之前,是不能调用它的方法的。必然会报NullPointerException.
      

  4.   

    在onCreate方法里面加一句l = new ArrayList<String>();就可以了
      

  5.   

    你太给力了
    我是只有一点C++基础
    看过Java基础,觉得边用边学会比较快,结果总是遇到这种问题!
    谢谢!