enter image description here我是android的新手,现在我通过udacity.com教程引用了sun app。代码是相同的,但仍然没有显示选项设置,并且没有在操作栏上刷新
我该如何解决我的问题?
主要活动。主菜单的java代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new ForecastFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和ForecastFragment.java用于刷新菜单
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
}
主菜单xml文件代码
<menu 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"
tools:context="com.example.android.sunshine.app.MainActivity" >
<item android:id="@+id/action_settings"
android:orderInCategory="100" />
app:showAsAction="never"
android:title="@string/action_settings"
android:orderInCategory="100" /></menu>
刷新菜单位于forecastfragment.xml文件代码中,refrsh菜单是
<item android:id="@+id/action_refresh"
app:showAsAction="never"
android:title="@string/action_refresh"
android:orderInCategory="100" />
清单文件是
<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
style.xml文件
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:icon">@drawable/ic_launcher</item>
</style>
<!--style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style-->
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
答案 0 :(得分:0)
在片段的gaierror occurred
err invoked
gaierror occurred
err invoked
gaierror occurred
err invoked
gaierror occurred
...
方法中写
onCreateView
并将此setHasOptionsMenu(true);
添加到您的super.onCreateOptionsMenu(menu, inflater);
onCreateOptionsMenu
您可以在public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.forecastfragment, menu);
}
activity_main.xml
这在您的<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
MainActivity.java
您的完整Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
应如下所示
公共类MainActivity扩展了AppCompatActivity {
MainActivity
}
如果您想要显示刷新菜单,则需要将此代码放入片段@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(mToolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//setHasOptionsMenu(true);
return rootView;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main, menu);
}
}
你setHasOptionsMenu(true);
就像这样
activity_main.xml
它应该有用......
答案 1 :(得分:0)
在你的menu.xml中你必须设置app:showAsAction =&#34; always&#34;它会显示出来。
答案 2 :(得分:0)
材料设计确实不支持ActionBar。因此,您无法看到ActionBar以及App图标。如果您想要ActionBar之类的功能,那么您可以使用类似以下代码的工具栏...
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
答案 3 :(得分:0)
从ActionBarActivity类扩展baseClass。
这应该解决这个问题。
public class MainActivity extends ActionBarActivity{
}