1

嗨,我正在尝试在膨胀视图中更改文本视图,但应用程序崩溃。

我要做的是用变量 HomeTeam 和 AwayTeam 中的内容填充文本视图,这些变量不为空,因为当我记录它们时,它们里面有名字。

所以我的问题是如何更改文本视图并替换其内容

这是我试过的

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);

膨胀的视图

<?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>

这是错误

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)
4

1 回答 1

10

你需要改变:

TextView homeTeam = (TextView) findViewById(R.id.HomeTeam);

对此:

TextView homeTeam = (TextView) resultsView.findViewById(R.id.HomeTeam);

为什么?因为HomeTeam在您的膨胀视图中可用。因此,您需要在查找子视图时参考您的膨胀视图/视图组

于 2012-05-18T14:10:21.430 回答