简体   繁体   中英

Activity main doesn't display title bar

i'm trying to develop a simple app including a Fragment (first time I use it ). The problem is the XML of my activity main does not display the title bar, maybe i'm doing something wrong with the Fragment,can't understand what.

MainActivity.java :

 public class MainActivity extends FragmentActivity {

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

       if(savedInstanceState==null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment, new ForecastFragment()).commit();
        }
    }
}

activity_main.xml :

    <FrameLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
    tools:context=".app.Activities.ForecastFragment">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
        android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
        tools:layout="@layout/fragment_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Fragment_main.xml :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".ForecastFragment"
tools:showIn="@layout/activity_main">


<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listview_forecast"
    android:layout_gravity="center" />

content_main.xml

<FrameLayout 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:id="@+id/fragment_forecast"
android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
 />

ForecastFragment.java

    public class ForecastFragment extends Fragment {

    public ForecastFragment() {}

    String[] forecastArray = {
            "Today - Sunny - 31/20",
            "Tomorrow - Sunny  - 42,30",
            "Wednesday - Rainy - 20,15",
            "Thursday - FINIMONDO - 35,20"
    };
    ArrayList<String> list = new ArrayList<>(Arrays.asList(forecastArray));

    @Override
    public void onCreate(Bundle savedIstanceState ){
        super.onCreate(savedIstanceState);

        setHasOptionsMenu(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View  rootView = inflater.inflate(R.layout.fragment_main, container,false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,list);
        ListView lw = (ListView)rootView.findViewById(R.id.listview_forecast);
        lw.setAdapter(adapter);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if( id == R.id.action_refresh ) {
            FetchWeatherTask fetchWeatherTask = new FetchWeatherTask();
            fetchWeatherTask.execute();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

logcat on start :

    08-10 12:22:55.397 2311-2311/com.example.sgrumo.sunshineonyourmind I/art: Not late-enabling -Xcheck:jni (already on)
08-10 12:22:55.397 2311-2311/com.example.sgrumo.sunshineonyourmind W/art: Unexpected CPU variant for X86 using defaults: x86
08-10 12:22:55.433 2311-2311/com.example.sgrumo.sunshineonyourmind W/System: ClassLoader referenced unknown path: /data/app/com.example.sgrumo.sunshineonyourmind-1/lib/x86
08-10 12:23:01.545 2311-2311/com.example.sgrumo.sunshineonyourmind W/System: ClassLoader referenced unknown path: /data/app/com.example.sgrumo.sunshineonyourmind-1/lib/x86
08-10 12:23:01.725 2311-2747/com.example.sgrumo.sunshineonyourmind I/OpenGLRenderer: Initialized EGL, version 1.4
08-10 12:23:01.725 2311-2747/com.example.sgrumo.sunshineonyourmind D/OpenGLRenderer: Swap behavior 1

After spending a couple of hours trying to debug my app, i've started again the project choosing an empty activity. Now everything is perfect!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM