Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid adding string to ListView
    primarykey
    data
    text
    <p>As part of a SMS app I am making, I need to add text from a TextView to a Row in a ListView dynamically as the TextView changes... adding another Row each time the TextView says something different.</p> <p>That being said, I created a small example project to try and get the hang of doing this, and I have looked up many examples, but still not working. Upon starting the app, it pauses for a bit and then crashes.</p> <p>I am trying to have the text from my TextView(id:tvContent) from the main.xml layout added to a row of the ListView.</p> <p>Here is my XML Layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;Button android:id="@+id/addBtn" android:text="Add New Item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="addItems" &gt; &lt;/Button&gt; &lt;TextView android:id="@+id/tvContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hey there!" &gt; &lt;/TextView&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" android:background="@color/black" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>My Activity Class:</p> <pre><code>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&lt;String&gt; listItems=new ArrayList&lt;String&gt;(); ArrayAdapter&lt;String&gt; 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&lt;String&gt;(this, android.R.layout.simple_list_item_1, listItems); setListAdapter(adapter); listItems.add(shareFact); adapter.notifyDataSetChanged(); } } </code></pre> <p>Here is my log cat... I couldn't figure out how to implement it in the question niceley...</p> <pre><code>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.&lt;init&gt;(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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload