2012-12-19 170 views
0

添加ListView项目经过几天的尝试,搜索和再次尝试我想问你们的帮助。通过点击一个按钮

我想通过点击随机用户输入项的按钮,以填补一个ListView。

这里是我的activity_main.xml中的一个例子图片

http://s1.directupload.net/file/d/3109/x6qnnj5s_png.htm

我试图severel代码段从这里和其他网站,但每次我的虚拟设备提示应用TEST_1(过程com.example.test_1 )已经意外停止。请再试一次。

我当前的代码是:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<ListView 
    android:id="@+id/tPlanView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/addTplan" > 
</ListView> 

<Button 
    android:id="@+id/addTplan" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:text="Button" /> 

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/textIn" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 
<TextView 
    android:id="@+id/textOut" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 

    /> 

MainActivity.java

public class MainActivity extends ListActivity { 

/** Items entered by the user is stored in this ArrayList variable */ 
ArrayList<String> list = new ArrayList<String>(); 

/** Declaring an ArrayAdapter to set items to ListView */ 
ArrayAdapter<String> adapter; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    /** Setting a custom layout for the list activity */ 
    setContentView(R.layout.activity_main); 

    /** Reference to the button of the layout main.xml */ 
    Button btn = (Button) findViewById(R.id.addTplan); 

    /** Defining the ArrayAdapter to set items to ListView */ 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); 

    /** Setting the event listener for the add button */ 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      EditText edit = (EditText) findViewById(R.id.textIn); 
      list.add(edit.getText().toString()); 
      edit.setText(""); 
      adapter.notifyDataSetChanged(); 

     } 
    }); 

    /** Setting the adapter to the ListView */ 
    setListAdapter(adapter); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

我很高兴的任何帮助。 在此先感谢媒体链接:-)

------- ------ UPDATE

logcat的

12-19 22:45:02.854: D/AndroidRuntime(330): Shutting down VM 
12-19 22:45:02.854: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-19 22:45:02.864: E/AndroidRuntime(330): FATAL EXCEPTION: main 
12-19 22:45:02.864: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{ de.example.test_1test_1/ de.example.test_1.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-19 22:45:02.864: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method) 
12-19 22:45:02.864: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507) 
12-19 22:45:02.864: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-19 22:45:02.864: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-19 22:45:02.864: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method) 
12-19 22:45:02.864: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
12-19 22:45:02.864: E/AndroidRuntime(330): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.Activity.setContentView(Activity.java:1657) 
12-19 22:45:02.864: E/AndroidRuntime(330): at de.example.test_1.MainActivity.onCreate(MainActivity.java:33) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-19 22:45:02.864: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
12-19 22:45:02.864: E/AndroidRuntime(330): ... 11 more 
12-19 22:45:05.964: I/Process(330): Sending signal. PID: 330 SIG: 9 
+0

请问您可以发布您的logcat吗? – Numair

+0

感谢您的快速响应。 logcat在问题 – atr

+2

中添加您正在将ListActivity与默认的ArrayAdapter一起使用,但是IIRC我认为您应该定义自己的自定义ArrayAdapter,以扩展包含TextView和EditText的list_item.xml。 –

回答

2

如果你打算在你的主要布局使用extends ListActivity那么ListView必须这样写这个ID:

<ListView 
    android:id="@android:id/list" <--------- 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/addTplan" > 
</ListView> 

至于主要问题询问,也许你会发现这个答案以下问题很有用,因为它涵盖了同一主题。

How to display item after clicked the button in eclipse?

1

变化

<ListView 
    android:id="@+id/tPlanView" 

<ListView 
    android:id="@android:id/list"