在充气另一个视图后Android更改textView

时间:2012-05-18 14:07:34

标签: android textview layout-inflater

您好我正在尝试在虚增的视图中更改文本视图,但应用程序崩溃了。

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

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

继承人我尝试过的事情

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)

1 个答案:

答案 0 :(得分:10)

您需要更改:

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

对此:

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

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