Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在布局内的TextView中动态设置文本-Android_Java_Android_Android Layout_Textview - Fatal编程技术网

Java 在布局内的TextView中动态设置文本-Android

Java 在布局内的TextView中动态设置文本-Android,java,android,android-layout,textview,Java,Android,Android Layout,Textview,我有这个布局,包含一个文本视图,但我希望能够使用java代码以动态方式更改它的内容(要可视化的文本) <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">

我有这个布局,包含一个
文本视图
,但我希望能够使用java代码以动态方式更改它的内容(要可视化的文本)

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <TextView 

        android:id="@+id/list_header"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#FFFFFF"
        android:padding="10dp"
        android:background="#336699" 

     />

</LinearLayout>
但这不起作用,因为我使用此布局定义列表标题的样式,并且这些行与我需要使用的以下行冲突:

View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
listView.addHeaderView(header);
你能帮我解决这个问题吗


LOGCAT:

E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 1468):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 1468):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1468):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 1468):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1468):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 1468):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 1468):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 1468):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1468): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DVA_HLUI/com.DVA_HLUI.DVA_HLUISuperviseActivity}: java.lang.NullPointerException
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
E/AndroidRuntime( 1468):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
E/AndroidRuntime( 1468):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
E/AndroidRuntime( 1468):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
E/AndroidRuntime( 1468):    at android.widget.TabHost.setCurrentTab(TabHost.java:346)
E/AndroidRuntime( 1468):    at android.widget.TabHost.addTab(TabHost.java:236)
E/AndroidRuntime( 1468):    at com.DVA_HLUI.DVA_HLUIActivity.onCreate(DVA_HLUIActivity.java:41)
E/AndroidRuntime( 1468):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 1468):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 1468):    ... 11 more
E/AndroidRuntime( 1468): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 1468):    at com.DVA_HLUI.DVA_HLUISuperviseActivity.onCreate(DVA_HLUISuperviseActivity.java:41)
E/AndroidRuntime( 1468):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 1468):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 1468):    ... 21 more
public class MyActivity extends ListActivity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);

        listEntryClass listEntries[] = new listEntryClass[]
        {
            new listEntryClass( "bla", "bla"),
            new listEntryClass( "bla", "bla" )
        };

        listEntryArrayAdapter adapter = new listEntryArrayAdapter(this, R.layout.list_entry_layout, listEntries);

        TextView headerValue = (TextView) findViewById(R.id.list_header);
        headerValue.setText( this.getString(R.string.headerSupervise) );

        View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
        listView.addHeaderView(header);
        listView.setAdapter(adapter);
}

活动类:

E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 1468):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 1468):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1468):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 1468):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1468):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 1468):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 1468):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 1468):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1468): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.DVA_HLUI/com.DVA_HLUI.DVA_HLUISuperviseActivity}: java.lang.NullPointerException
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
E/AndroidRuntime( 1468):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
E/AndroidRuntime( 1468):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
E/AndroidRuntime( 1468):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
E/AndroidRuntime( 1468):    at android.widget.TabHost.setCurrentTab(TabHost.java:346)
E/AndroidRuntime( 1468):    at android.widget.TabHost.addTab(TabHost.java:236)
E/AndroidRuntime( 1468):    at com.DVA_HLUI.DVA_HLUIActivity.onCreate(DVA_HLUIActivity.java:41)
E/AndroidRuntime( 1468):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 1468):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 1468):    ... 11 more
E/AndroidRuntime( 1468): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 1468):    at com.DVA_HLUI.DVA_HLUISuperviseActivity.onCreate(DVA_HLUISuperviseActivity.java:41)
E/AndroidRuntime( 1468):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime( 1468):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 1468):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 1468):    ... 21 more
public class MyActivity extends ListActivity 
{

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

        ListView listView = (ListView) findViewById(android.R.id.list);

        listEntryClass listEntries[] = new listEntryClass[]
        {
            new listEntryClass( "bla", "bla"),
            new listEntryClass( "bla", "bla" )
        };

        listEntryArrayAdapter adapter = new listEntryArrayAdapter(this, R.layout.list_entry_layout, listEntries);

        TextView headerValue = (TextView) findViewById(R.id.list_header);
        headerValue.setText( this.getString(R.string.headerSupervise) );

        View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
        listView.addHeaderView(header);
        listView.setAdapter(adapter);
}
应从标题视图中引用

因此,将代码更改为folling

    View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);
    TextView headerValue = (TextView) header.findViewById(R.id.list_header);
    headerValue.setText( this.getString(R.string.headerSupervise) );

    listView.addHeaderView(header);
    listView.setAdapter(adapter);
使用

而不是

TextView headerValue = (TextView) findViewById(R.id.list_header);
headerValue.setText( this.getString(R.string.headerSupervise) );

View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);

我想我明白了:我是从上下文而不是从视图标题获取TextView headerValue的。对吗?非常感谢你的及时;D更正!!如果它解决了你的问题,那么就结束这个问题。快乐
TextView headerValue = (TextView) findViewById(R.id.list_header);
headerValue.setText( this.getString(R.string.headerSupervise) );

View header = (View)getLayoutInflater().inflate(R.layout.list_header_layout, null);