Java 在android中设置TextView的可见性不工作

Java 在android中设置TextView的可见性不工作,java,android,xml,listview,textview,Java,Android,Xml,Listview,Textview,当我试图使视图“消失”时,我的程序遇到运行时异常。 我处理它的代码: setContentView(R.layout.list_main); ... lv = getListView(); View header = (View) findViewById(R.id.header_layout_root); TextView tv = (TextView) header.findViewById(R.id.new_highscore);

当我试图使视图“消失”时,我的程序遇到运行时异常。 我处理它的代码:

setContentView(R.layout.list_main);
... 
 lv =  getListView();
             View header = (View) findViewById(R.id.header_layout_root);
             TextView tv = (TextView) header.findViewById(R.id.new_highscore);
            tv.setVisibility(View.GONE);

 lv = getListView();
        LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
        lv.addHeaderView(header, null, false);

mC = mDBHelper.getAllHighScores();
        startManagingCursor(mC);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, mC, 
                new String[] { DBAdapter.COL_NAME, DBAdapter.COL_SCORE }, 
                new int[] { R.id.txtText1, R.id.txtText2 });
        lv.setAdapter(adapter);
xml:

我认为,问题在于,新的\u highscore不在list\u main中,而是在另一个xml文件中。我使用两个,因为我有一个标题和图像等列表视图

有没有什么想法可以让风景“看不见”或“消失”呢

谢谢


Ben

你就不能只有一个xml,下面是文本视图和列表视图。那么它应该可以工作了,难道你就不能只有一个xml和一个textview和一个listview吗。那么它应该可以工作了

试试这段代码

从顶部删除此代码

如果这不起作用,请发布您遇到的异常

试试这段代码

从顶部删除此代码


如果这不起作用,请发布您遇到的异常

如何将xml文件与R.id.new\u highscore一起使用?如何将xml文件与R.id.new\u highscore一起使用?我尝试了这个,它不起作用。。。我已经更新了代码来显示我是如何分配ListViews的谢谢Justin,我已经更改了代码,似乎还出现了一些其他错误。不是直接访问ie view highscores(查看高分)按钮,而是在我添加分数时,将变量传递给新活动,然后显示高分。我得到了这些错误,它是在starttAcvtvity之后和后续onCreate.WARN/WindowManager1377之前:上一个调度状态:{{KeyEvent{action=1 code=4 repeat=0 meta=0 scancode=158 mFlags=8}到窗口{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}@1319733312935 lw=Window{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}lb=android.os。BinderProxy@45004928fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}}错误是我的错误,在清单中。你的解决方案拯救了这一天:如果我能做两次投票,你今天就会得到它。多谢老兄!投票了一次。我尝试了这个,它不起作用…我已经更新了代码以显示我如何分配ListViewThank Justin,我已经更改了代码,一些其他错误似乎正在出现。当acce单击“查看高分”按钮,但当我添加分数时,将变量传递给新活动,然后显示高分。我得到这些错误,是在开始tTACVTvity之后,在随后的onCreate之前。WARN/WindowManager1377:上一个调度状态:{{KeyEvent{action=1 code=4 repeat=0 meta=0 scancode=158 mFlags=8}到窗口{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}@1319733312935 lw=Window{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}lb=android.os。BinderProxy@45004928fin=false gfw=true ed=true tts=0 wf=false fp=false mcf=Window{45004ae8 com.test.frequencyTest/com.test.frequencyTest.Menu paused=false}}错误是我的错误,在清单中。你的解决方案挽救了这一天:如果我能做两次投票,你今天就可以得到。多谢你,老兄!投票了一次。我想以实现的方式使用LayoutInflater。我想以实现的方式使用LayoutInflater
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/header_layout_root"
  android:background="#ffffff"
  android:orientation="vertical">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick Master High Scores"
    android:background="#000000"
    android:textColor="#ffffff"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">
</TextView>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:id="@+id/header_layout_root1">
    <ImageView
        android:id="@+id/sample_image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/pickmaster"
        android:scaleType="fitXY">
    </ImageView>
    <TextView
        android:id="@+id/new_highscore"
        android:text="New High Score added!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffffff"
        android:textSize="12dp"
        android:background="#AA000000"
        android:padding="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignBottom="@id/sample_image">
    </TextView>
</RelativeLayout>
   lv = getListView();
   LayoutInflater inflater = getLayoutInflater();
   View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
   lv.addHeaderView(header, null, false);
   View relativeLayout = header.findViewById(R.id.header_layout_root1);
   TextView highScore = relativeLayout.findViewById(R.id.new_highscore);
   highScore.setVisibility(View.GONE);
lv =  getListView();
View header = (View) findViewById(R.id.header_layout_root);
TextView tv = (TextView) header.findViewById(R.id.new_highscore);
tv.setVisibility(View.GONE);