2016-07-21 5 views
1

Update: Ich repariere es, indem ich MainActivityFragment extends FragmentActivity zu MainActivityFragment extends AppCompatActivity ändere.Aktionsleiste wird wegen Absicht nicht angezeigt?

Danke Jungs.


Die Aktionsleiste wird zeigen, ob ich die Absicht nicht kommentieren. Wenn ich die Absicht in onCreate MainActivity ausführen, wird die Aktionsleiste nicht angezeigt. Wie zeige ich die Aktionsleiste mit der Absicht? Ich stelle den Intent (Hauptaktivität) und den Android Manifest-Code unten bereit.

Link zum Screenshot:

Non-Intent

Intent

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.activity_main); // this is the start  layout 
    Intent intent = new Intent(this, MainActivityFragment.class); 
    startActivity(intent); 
} 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.romi1.popularmoviesapp"> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <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"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <dependency> 
      <groupId> 
       info.movito 
      </groupId> 

      <artifactId> 
       themoviedbapi 
      </artifactId> 

      <version> 
       1.3 
      </version> 
     </dependency> 

     <activity android:name=".MainActivityFragment"></activity> 
     <activity android:name=".MoviesDetailAdapter"></activity> 
    </application> 

</manifest> 

Style.xml:

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> 
     <!--Theme.AppCompat.Light.DarkActionBar--> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

</resources> 

MainActivityFragment:

public class MainActivityFragment extends FragmentActivity { 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.fragment_main); 

     movies = new Movies[1000]; 

     jsonParser(json); 

     moviesAdapter = new MoviesAdapter(this, movies); 

     setContentView(R.layout.fragment_main); 
     GridView gridView = (GridView) findViewById(R.id.listView); 
     gridView.setAdapter(moviesAdapter); 

     // This for setting what happen when one of the movie is selected 
     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       // Make another fragment class for this Adapter 
       // And figure out away to send the clicked parcelable variable 
       Movies topRatedMovieJson = moviesAdapter.getItem(i); 
       Log.v(LOG_TAG, "topRatedMovieJson : " + i); 
       Log.v(LOG_TAG, "topRatedMovieJson : " + topRatedMovieJson.title.toString()); 
       Intent intent = new Intent(getBaseContext(), MoviesDetailAdapter.class); 
       intent.putExtra("parcelable", topRatedMovieJson); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

Update: ich das Problem beheben, indem MainActivityFragment extends FragmentActivity-MainActivityFragment extends AppCompatActivity ändern.

Danke Jungs.

+0

Ich glaube, Sie haben nicht ActionBar in MainActivityFragment. Außerdem ist es ziemlich verwirrend, eine Aktivität mit dem Namen 'MainActivityFragment' zu haben. Vielleicht wollten Sie Fragment hinzufügen, nicht Aktivität? –

+0

Vielen Dank Sergey. Sorry wegen der Benennung, ich werde es ändern. Das MainActivityFragment erweitert die Fragmentaktivität. Könnte das die Ursache sein? Wenn das der Fall ist, obwohl das Thema der App bereits im Android-Manifest eingerichtet wurde, muss ich die Aktionsleiste manuell zu MainActivityFragment hinzufügen. Ist das richtig? – QuartZ

+0

können Sie uns Ihre style.xml Datei zeigen – tk1505

Antwort

1

Es gibt 2 Möglichkeiten, dies zu tun. 1. Ändern Sie diese Aktivität in das Fragment 2. Fügen Sie ActionBar dem Layout der zweiten Aktivität und Ihrer Aktivität hinzu.

+0

Danke tk. Bedeutet Ihre zweite Option, dass Sie sie im Android-Manifest hinzufügen? – QuartZ

+0

Nein, Sie müssen Änderungen in Ihrer Datei layout.xml und in der MainActivityFragment-Aktivität vornehmen. Folgen Sie diesem Beispiel http://www.vogella.com/tutorials/AndroidActionBar/article.html – tk1505

+0

Danke tk. Ich repariere es, indem ich MainActivityFragment erweitert Fragment Aktivität zu AppCompatActivity. – QuartZ

0

Sie können zwei Stile festlegen und diese auf Ihre Aktivitäten anwenden.

beschreiben Stil wie folgt aus:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="SecondTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

und es auf Aktivitäten:

<activity android:name=".MainActivityFragment" android:theme="@style/SecondTheme"/> 
    <activity android:name=".MoviesDetailAdapter" android:theme="@style/SecondTheme"/> 
+0

Danke Lionel. Ich repariere es, indem ich MainActivityFragment erweitert Fragment Aktivität zu AppCompatActivity. – QuartZ