Reputation: 47
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.
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.
I am trying to have the text from my TextView(id:tvContent) from the main.xml layout added to a row of the ListView.
Here is my XML Layout:
<?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>
My Activity Class:
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();
}
}
Here is my log cat... I couldn't figure out how to implement it in the question niceley...
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
Upvotes: 2
Views: 33323
Reputation: 2535
You want to get the TextView before you inflate your layout, so it's a simple NullPointerExecption. Change your code:
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();
}
}
If you press the button, it will call the addItems() function (as you set it), but it's not implemented yet, so it will generate an other error.
public void addItems(View v) {
listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
Add this function below your onCreate() method, and move the listItems.add() and adapter.notifyDataSetChanged() lines to this new method.
Upvotes: 7