我有edittext和按鈕和列表視圖,我想從edittext添加項目到列表視圖,當我點擊從edittext按鈕文本添加到列表視圖,但會出現此錯誤「不幸的是,列表已停止「。將項目添加到列表視圖編輯文本
> main.xml中的代碼是:
<EditText
android:id="@+id/EdtInput"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:hint="@string/StInput"
/>
<Button
android:id="@+id/BtnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/StAdd"
android:layout_toRightOf="@+id/EdtInput"
/>
<ListView
android:id="@+id/LstItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/EdtInput"
android:layout_alignLeft="@+id/EdtInput"
></ListView>
> MainActivity.java代碼是:
包com.saly.list;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText ET=(EditText) findViewById(R.id.EdtInput);
final String StInput=ET.getText().toString();
final ListView LV=(ListView) findViewById(R.id.LstItem);
final ArrayList<String> ArLi=new ArrayList<String>();
final ArrayAdapter<String> ArAd=new ArrayAdapter<String>(this, R.layout.main,ArLi);
LV.setAdapter(ArAd);
Button Add=(Button) findViewById(R.id.BtnAdd);
Add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ArLi.add(StInput);
ArAd.notifyDataSetChanged();
ET.setText("");
}
});
}
}
這是logcat的出錯:
02-27 16:44:06.369: I/Adreno-EGL(21719): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
02-27 16:44:06.409: D/OpenGLRenderer(21719): Enabling debug mode 0
02-27 16:44:12.515: E/ArrayAdapter(21719): You must supply a resource ID for a TextView
02-27 16:44:12.515: D/AndroidRuntime(21719): Shutting down VM
02-27 16:44:12.515: W/dalvikvm(21719): threadid=1: thread exiting with uncaught exception (group=0x41836ba8)
02-27 16:44:12.525: E/AndroidRuntime(21719): FATAL EXCEPTION: main
02-27 16:44:12.525: E/AndroidRuntime(21719): Process: com.saly.list, PID: 21719
02-27 16:44:12.525: E/AndroidRuntime(21719): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ListView.fillDown(ListView.java:691)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ListView.fillFromTop(ListView.java:752)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ListView.layoutChildren(ListView.java:1616)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.Choreographer.doFrame(Choreographer.java:544)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.os.Handler.handleCallback(Handler.java:733)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.os.Handler.dispatchMessage(Handler.java:95)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.os.Looper.loop(Looper.java:136)
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-27 16:44:12.525: E/AndroidRuntime(21719): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:44:12.525: E/AndroidRuntime(21719): at java.lang.reflect.Method.invoke(Method.java:515)
02-27 16:44:12.525: E/AndroidRuntime(21719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-27 16:44:12.525: E/AndroidRuntime(21719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-27 16:44:12.525: E/AndroidRuntime(21719): at dalvik.system.NativeStart.main(Native Method)
02-27 16:44:12.525: E/AndroidRuntime(21719): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
02-27 16:44:12.525: E/AndroidRuntime(21719): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
02-27 16:44:12.525: E/AndroidRuntime(21719): ... 40 more
在附註中,值得思考如何命名變量 - http://stackoverflow.com/questions/414001/vari能夠命名約定在java –