Reputation: 2402
Hi i'm trying to change a textview in an inflated view but the app crashes.
What i'm trying to do is fill the text views with the contents from the variables HomeTeam and AwayTeam these variables aren't null because when i log them the have names in there.
So my question is how do i change a text view and replace its contents
heres what I've tried
for(int t = 0; t < 1; t++){
resultsDict = jArray.getJSONObject(t);
String HomeTeam = resultsDict.getString("hometeam");
String AwayTeam = resultsDict.getString("awayteam");
Log.v("lc","hometeam" + HomeTeam);
Log.v("lc","awayteam" + AwayTeam);
}
resultsView = LayoutInflater.from(getBaseContext()).inflate(R.layout.resultscell,
null);
TextView homeTeam = (TextView) findViewById(R.id.HomeTeam);
homeTeam.setText(HomeTeam);
the inflated view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="20dp" >
<TextView
android:id="@+id/TextView04"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="-"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/HomeScore"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/TextView04"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/AwayScore"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/TextView04"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/AwayTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/AwayScore"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/AwayScore"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/blue"
android:textStyle="bold"
/>
<TextView
android:id="@+id/HomeTeam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/HomeScore"
android:layout_alignBottom="@+id/HomeScore"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/blue"
android:textStyle="bold"
/>
</RelativeLayout>
heres the error
05-18 13:55:57.552: E/AndroidRuntime(3987): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.myApp.MyApp/co.uk.myApp.MyApp.HomeActivity}: java.lang.NullPointerException
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.os.Looper.loop(Looper.java:143)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread.main(ActivityThread.java:4263)
05-18 13:55:57.552: E/AndroidRuntime(3987): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 13:55:57.552: E/AndroidRuntime(3987): at java.lang.reflect.Method.invoke(Method.java:507)
05-18 13:55:57.552: E/AndroidRuntime(3987): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-18 13:55:57.552: E/AndroidRuntime(3987): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-18 13:55:57.552: E/AndroidRuntime(3987): at dalvik.system.NativeStart.main(Native Method)
05-18 13:55:57.552: E/AndroidRuntime(3987): Caused by: java.lang.NullPointerException
05-18 13:55:57.552: E/AndroidRuntime(3987): at co.uk.myApp.MyApp.HomeActivity.onCreate(HomeActivity.java:576)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-18 13:55:57.552: E/AndroidRuntime(3987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
Upvotes: 1
Views: 3931
Reputation: 68187
You need to change:
TextView homeTeam = (TextView) findViewById(R.id.HomeTeam);
To this:
TextView homeTeam = (TextView) resultsView.findViewById(R.id.HomeTeam);
why? because HomeTeam is available in your inflated view. So, you need to refer to your inflated view/viewgroup when finding the child view
Upvotes: 10