Android将字符串添加到ListView

Cal*_*leb 0 java xml android listview textview

作为我正在制作的SMS应用程序的一部分,我需要在TextView更改时动态地将文本从TextView添加到ListView中的行...每次TextView说出不同的内容时添加另一行.

话虽这么说,我创建了一个小的示例项目来尝试并做到这一点,我已经查找了许多示例,但仍然没有工作.启动应用程序后,它会暂停一段时间然后崩溃.

我试图将我的TextView(id:tvContent)中的文本从main.xml布局添加到ListView的一行.

这是我的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"
android:background="@color/black" >

<Button
    android:id="@+id/addBtn"
    android:text="Add New Item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="addItems" >
</Button>

<TextView
    android:id="@+id/tvContent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hey there!" >     
</TextView>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:background="@color/black" >     
</ListView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的活动班:

package com.example.addtolistview;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListViewDemo extends ListActivity {

ArrayList<String> listItems=new ArrayList<String>();

ArrayAdapter<String> adapter;

TextView theFact = (TextView) findViewById(R.id.tvContent);
String shareFact = theFact.getText().toString();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            listItems);
    setListAdapter(adapter);

    listItems.add(shareFact);
    adapter.notifyDataSetChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的日志猫...我无法弄清楚如何在问题中实现它...

D/AndroidRuntime(11420): Shutting down VM
W/dalvikvm(11420): threadid=1: thread exiting with uncaught exception (group=0x40c5fa68)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.addtolistview/com.example.addtolistview.ListViewDemo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1794)
at com.example.addtolistview.ListViewDemo.<init>(ListViewDemo.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
... 11 more
Run Code Online (Sandbox Code Playgroud)

nis*_*v4n 6

您希望在扩充布局之前获取TextView,因此它是一个简单的NullPointerExecption.更改您的代码:

public class ListViewDemo extends ListActivity {

    ArrayList<String> listItems=new ArrayList<String>();
    ArrayAdapter<String> adapter;

    TextView theFact;
    String shareFact;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        theFact = (TextView) findViewById(R.id.tvContent)
        shareFact = theFact.getText().toString();

        adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        listItems);
        setListAdapter(adapter);

        listItems.add(shareFact);
        adapter.notifyDataSetChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果按下按钮,它将调用addItems()函数(如您设置的那样),但它尚未实现,因此会产生其他错误.

public void addItems(View v) {
    listItems.add(shareFact);
    adapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)

在onCreate()方法下添加此函数,并将listItems.add()和adapter.notifyDataSetChanged()行移动到此新方法.