2014-07-09 134 views
4

操作欄上定位按鈕Action Bar Back Button在仿真器工作正常,但在設備不工作任何一個可以幫助我Android操作欄,導航向上按鈕不工作的設備

MainActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.mainpage); 

    tf = Typeface.createFromAsset(getAssets(), "font/AGENCYB.TTF"); 

    Button btnWeatherDetails = (Button) findViewById(R.id.btnWeather); 
    Button btnMovieDetails = (Button) findViewById(R.id.btnMovies); 
    Button btnHoroscopeDetails = (Button) findViewById(R.id.btnHoroscope); 

    btnWeatherDetails.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) {    
      Intent i = new Intent(MainActivity.this, WeatherActivity.class); 
      MainActivity.this.startActivity(i); 
     } 
    }); 


    btnMovieDetails.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent i = new Intent(MainActivity.this, MovieActivity.class); 
      MainActivity.this.startActivity(i);    
     } 
    }); 

    btnHoroscopeDetails.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent i = new Intent(MainActivity.this, HoroscopeActivity.class); 
      MainActivity.this.startActivity(i);  
     } 
    }); 

} 

@Override 
protected void onDestroy() { 

    SharedPreferences settingsPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE); 
    SharedPreferences.Editor settingsEditor= settingsPreferences.edit(); 
    settingsEditor.putBoolean(MovieActivity.MOVIEUPDATED, false); 
    settingsEditor.putBoolean(HoroscopeActivity.HOROSCOPEAVAILABLE, false); 
    settingsEditor.commit(); 

    super.onDestroy(); 
} 

WeatherActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scrollmain); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); 
    SavedString = settingsPreferences.getString(JSONWEATHERSTRING, "Not Found"); 

    new JSONParse().execute(WeatherUrl); 

} 

public boolean onCreateOptionMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


public boolean onOptionItemSelected(MenuItem item) 
{ 
    switch(item.getItemId()){ 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void onBackPressed() 
{ 
    moveTaskToBack(true); 
    WeatherActivity.this.finish(); 
} 

MovieActivity

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); 

    MovieJsonString = settingsPreferences.getString(JSONMOVIESTRING, "Not Found"); 
    isMovieUpdated = settingsPreferences.getBoolean(MOVIEUPDATED, false); 

    setContentView(R.layout.main_list_view);   
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    movieList = new ArrayList<HashMap<String, String>>(); 

    list = getListView(); 
    // Calling async task to get json 
    if(!isMovieUpdated) 
     new GetMovieDetails().execute(); 
    else 
     GetMoviePreferences(); 

} 

public boolean onCreateOptionMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


public boolean onOptionItemSelected(MenuItem item) 
{ 
    switch(item.getItemId()){ 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void onBackPressed() 
{ 
    moveTaskToBack(true); 
    MovieActivity.this.finish(); 
} 

HoroscopeActivity

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.horoscope); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); 

    isHoroAvailable = settingsPreferences.getBoolean(HOROSCOPEAVAILABLE,false); 

    if(!isHoroAvailable) 
     new XMLParse().execute(); 
    else 
     GetHoroscopeFromPreference(); 
} 

public boolean onCreateOptionMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


public boolean onOptionItemSelected(MenuItem item) 
{ 
    switch(item.getItemId()){ 
    case android.R.id.home: 
     NavUtils.navigateUpFromSameTask(this); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

public void onBackPressed() 
{ 
    moveTaskToBack(true); 
    HoroscopeActivity.this.finish(); 
} 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="in.datumdata.weather" 
    android:installLocation="auto" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_hosurdata" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="in.datumdata.hosurdata.MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="in.datumdata.hosurdata.MovieActivity" 
      android:label="Movie Details" 
      android:parentActivityName="in.datumdata.hosurdata.MainActvity" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name="in.datumdata.hosurdata.WeatherActivity" 
      android:label="Weather Details" 
      android:parentActivityName="in.datumdata.hosurdata.MainActvity" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name="in.datumdata.hosurdata.SingleContactActivity" 
      android:label="Movie Details" 
      android:parentActivityName="in.datumdata.hosurdata.MovieActivity" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name="in.datumdata.hosurdata.HoroscopeActivity" 
      android:label="Horoscope Details" 
      android:parentActivityName="in.datumdata.hosurdata.MainActvity" 
      android:screenOrientation="portrait" /> 
     <activity 
      android:name="in.datumdata.hosurdata.SingleHoroscopeActivity" 
      android:label="Horoscope Details" 
      android:parentActivityName="in.datumdata.hosurdata.HoroscopeActivity" 
      android:screenOrientation="portrait" /> 
    </application> 

</manifest> 

在所有上述活動的導航按鈕Action Bar Back Button工作正常,在模擬器,但在設備上它不工作,當我按下設備上的後退按鈕enter image description here應用程序不可見我的意思是說它在後臺運行。這件事情發生在任何活動上,我按回來按鈕發生的所有活動

幫助我。預先感謝併爲我的英語不好而感到遺憾。

回答

3

如果你這樣做:

getActionBar().setDisplayHomeAsUpEnabled(true); 

試着這樣做:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
7

您應該使用向上按鈕元數據標籤的工作對舊的API:

 android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="MainActivity" /> 

編輯:

你已經有parentActivityName,所以纔有了元標記添加到您的AndroidManifest.xml文件的標籤。像這樣:

<activity 
     android:name="in.datumdata.hosurdata.MovieActivity" 
     android:label="Movie Details" 
     android:parentActivityName="in.datumdata.hosurdata.MainActvity" 
     android:screenOrientation="portrait"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="in.datumdata.hosurdata.MainActvity" /> 
    </activity> 
+0

我應該在每項活動中添加此項y – Prashanth

+0

致所有應該有**向上按鈕**的活動。還要更改活動名稱,以便它們與應該調用的名稱相匹配。 – Simas

+0

對不起din改變活動名稱? – Prashanth

1

我有支持動作條的問題,如果你使用的支持動作條,那麼你已經在你的代碼中添加這一行。

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

而且在您需要添加

<meta-data 
    android:name="android.support.PARENT_ACTIVITY" 
    android:value="com.example.ParentActivity" /> 

所以在清單中的活動宣言將類似於清單:

<activity 
    android:name="com.example.MyActivity" 
    android:label="My Activity"> 
    <meta-data 
     android:name="android.support.PARENT_ACTIVITY" 
     android:value="com.example.ParentActivity" /> 
</activity> 

在你不能在活動聲明添加android:parentActivityName="com.example.ParentActivity"如果您使用的API級別低於16,則會顯示。

相關問題