Android 更改 textView 后膨胀另一个View

标签: Android textview
发布时间: 2014/9/20 8:28:25
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

嗨我试图改变 textview 在膨胀 view 但应用程序崩溃。

什么要做的就是从翌年变量的内容填充文本视图和 AwayTeam 这些变量不是 null 因为当我 log 他们把名字放在那里。

所以我的问题是如何更改的文本 view 和替换其内容

下面是我试过什么

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

膨胀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>

下面是错误

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:

您需要更改:

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

更改为:

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

为什么呢?因为翌年是可用在你膨胀 view 。所以,你需要找到这个子元素的时候是指你膨胀的视图/分组view

赞助商