2016-06-12 3 views
0

問題:アプリケーションを起動すると、アクションバーにアプリケーションのタイトル「ParseC」が正しく表示されます(コンテンツ領域はまだ空白です)。 RSSを読み込んでカスタムリストビューを設定した後、タイトルが消えます。これはAPI 16と23で同じ効果でテストされました。Androidのアクションバーのタイトルがカスタム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)には、customレイアウトitemlistrow.xmlを使用するlistviewを持つcontent_main.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()に2度も電話する必要はありません(これはあなたの問題に寄与している可能性があります)。一度はonCreate()にする必要があります。これは、2度呼び出さずに、データが受信されたときにアダプタでnotifyDataSetChanged()を呼び出すと、アプリのパフォーマンスが向上します。 onCreate()でアダプタを設定することをお勧めします。データが受信されたときに更新するだけです。

+0

ありがとうございます。私の具体的な問題は、setContentView()が2回目に呼び出されたことです(理由を理解したいと思います)。以前はそれを置くとnullポインタの例外が出てしまったので、私はpostexecuteにアダプタを保持しました。 –

+0

'setContentView()'は明示的にビューの階層構造を取り、指定されたビューに置き換えます。したがって、アクションバーで行った作業はすべて失われます。ヌルポインタについては、あなたは 'onCreate()'でアダプタを作成していて、それをインスタンス変数にしていますか?それは、おそらくより効率的で、必要に応じてアクティビティにアクセスできるでしょう。ヌルポインタは 'setContentView()'を使って2回接続することもできます。 –

関連する問題