不调用OnCreate方法(Sunshine app)

时间:2016-12-22 11:43:04

标签: java android

我正在使用Android自学Sunshine应用程序,而且我在操作栏中无法获得额外的菜单条目。

我发现的最可能的原因是我的帧类的OnCreate方法没有被调用,因此菜单没有被夸大。

这是我的MainActivity代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("MainActivity","OnCreate Started 1");
    if (savedInstanceState == null) {
        Log.d("MainActivity","OnCreate Started 2");
        ForecastFragment MyFragment = new ForecastFragment();
        if (MyFragment == null){
            Log.d("MainActivity","OnCreate Started 3");
        }
        else{
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, MyFragment)
                .commit();}
    }
}

现在是我的ForecastFragment类的代码:

public class ForecastFragment extends Fragment {

public ForecastFragment() {
}

//@Override
protected void OnCreate(Bundle savedInstanceState){
    Log.d("Forecastfragment","OnCreate Started");
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu; this adds items to the action bar if it is present.

    inflater.inflate(R.menu.forecastfragment, menu);

}

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_refresh) {
        FetchWeatherTask WeatherTask = new FetchWeatherTask();
        WeatherTask.execute();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

当我运行该应用时,我看不到正在调用OnCreate类的ForecastFragment方法的日志记录。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这不是Fragment

的确切继承的onCreate方法
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
    }

它应该是onCreate而不是OnCreate