2016-06-12 72 views
0

我的问题是:启动应用程序时,只有一个活动,操作栏正确显示应用程序标题“ParseC”(虽然内容区域仍为空白),但加载RSS并填充自定义列表视图后,标题消失。这在API 16和23上进行了测试,效果相同。Android Action Bar标题在填充自定义ListView后消失

onCreate()解析RSS提要;当它完成时,onPostExecute用条目填充列表视图,此时应用栏标题消失。 如果我运行getSupportActionBar()。getTitle()。toString(),我确实得到了正确答案(ParseC)。

这是我MainActivity.java(的onCreate):

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    context = this; 

    new DownloadXmlTask().execute(URL); 
} 

和(onPostExecute):

@Override 
protected void onPostExecute(String result) { 
    setContentView(R.layout.activity_main); 

    //Adapter code 
    ListView yourListView = (ListView) findViewById(R.id.listView); 
    // get data from the table by the ListAdapter 
    ListAdapter customAdapter = new ListAdapter(context, R.layout.itemlistrow, theEntries); 
    yourListView .setAdapter(customAdapter); 
} 

我预计是为标题呆在那里,因为我没有离开活动(我怀疑错误可能在布局上发生)。

至于布局,主要活动布局(activity_main.xml)包含content_main.xml,它具有使用自定义布局itemlistrow.xml的列表视图。

我的问题是:什么可能导致应用栏标签被隐藏?

的AndroidManifest.xml下面:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="co.samoliver.parsec"> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="co.samoliver.parsec.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<include layout="@layout/content_main" /> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_info" /> 

布局: content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="co.samoliver.parsec.MainActivity" 
tools:showIn="@layout/activity_main"> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/listView" /> 

itemlistrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" android:orientation="vertical" 
android:layout_width="fill_parent"> 

<TextView android:textColor="#000000" 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Title" android:textStyle="bold" 
    android:gravity="center_vertical" 
    android:layout_weight="1" 
    android:typeface="normal" 
    android:height="40sp" 
    android:textSize="16dp" /> 


<TextView android:textColor="#000000" 
    android:id="@+id/summary" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Summary" 
    android:layout_weight="1" 
    android:height="20sp" 
    android:typeface="normal" /> 

<TextView android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_weight="1" 
android:textColor="#000000" 
android:gravity="right" 
android:id="@+id/id" 
android:text="Id" 
android:height="0sp" 
android:typeface="normal" /> 

+0

如果您怀疑布局上可能出现错误,请发布布局请 –

回答

1

的错误可能是在您的清单android:theme="@style/AppTheme.NoActionBar"。此外,您不需要拨打setContentView()两次(这也可能会导致您的问题)。一旦在onCreate()应该做。如果您不调用它两次,它会提高您的应用程序的性能,而在收到数据时调用适配器上的notifyDataSetChanged()。我会建议在onCreate()本身设置您的适配器,并在收到数据时更新它。

+0

谢谢。我的具体问题是setContentView()被第二次调用(我想明白为什么)。我将适配器保留在postexecute上,因为之前提供了一个空指针异常。 –

+0

'setContentView()'将显式地采用视图层次结构并将其替换为给定的视图。因此,您使用该操作栏所做的所有工作都将丢失。对于空指针,你是在'onCreate()'中创建适配器,并且可能使它成为一个实例变量?在需要的时候,这可能会更有效率并且可供活动使用。空指针也可以使用'setContentView()'连接两次。 –