Action Bar因意图而无法显示?

时间:2016-07-21 18:33:30

标签: java android android-intent

更新 我通过将MainActivityFragment extends FragmentActivity更改为MainActivityFragment extends AppCompatActivity来解决此问题。

谢谢你们。


如果我不对意图发表评论,操作栏将显示。如果我在onCreate MainActivity中运行Intent,则Action Bar不会显示。如何使用Intent显示Action Bar。我在下面提供了Intent(主要活动)和Android Manifest代码。

链接到屏幕截图:

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);
            }
        });
    }
}

更新 我通过将MainActivityFragment extends FragmentActivity更改为MainActivityFragment extends AppCompatActivity来解决此问题。

谢谢你们。

2 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点 1.将该活动更改为片段 2.将Actionbar添加到第二个活动的布局和活动中。

答案 1 :(得分:0)

您可以设置两种样式并将其应用于您的活动。

描述这样的风格:

<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>

并将其应用于活动:

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