我正在粘贴我创建的所有三个java类,以便在AppCompatActivity中的片段中创建一个imagegrid,但是我得到了下面粘贴的显示错误。
我的主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.main_fragment, new MainActivityFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_sort) {
showPop();
return true;
}
return super.onOptionsItemSelected(item);
}
public void showPop()
{
View menuItemView = findViewById(R.id.action_sort);
PopupMenu popup = new PopupMenu(MainActivity.this, menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.sort_popup, popup.getMenu());
popup.show();
}
}
使用gridview的片段:
public class MainActivityFragment extends Fragment
{
public MainActivityFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_main, null);
GridView gv1=(GridView)root.findViewById(R.id.grid);
gv1.setAdapter(new ImageAdapter(getActivity()));
return root;
}
}
imageadapter类:
public class ImageAdapter extends BaseAdapter
{
private Context ctx;
public ImageAdapter(Context c)
{
ctx=c;
}
@Override
public int getCount()
{
return pics.length;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView iv;
if (convertView == null)
{
iv = new ImageView(ctx);
iv.setLayoutParams(new GridView.LayoutParams(350,350));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setPadding(4, 4, 4, 4);
}
else
{
iv = (ImageView)convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
private Integer[] pics={
R.drawable.a,R.drawable.b,
R.drawable.c,R.drawable.d,
R.drawable.e,R.drawable.f,
R.drawable.g,R.drawable.h
};
}
我收到此错误日志:
08-20 21:50:15.218 16512-16512/com.example.android.mymovieapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.mymovieapp, PID: 16512
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.mymovieapp/com.example.android.mymovieapp.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.android.mymovieapp.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
这是异常错误。尝试围绕你的代码try / catch,
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.android.mymovieapp / com.example.android.mymovieapp.MainActivity}: java.lang.IllegalStateException:此Activity已经有一个动作 酒吧提供的窗户装饰。不要求 Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false 您的主题是使用工具栏。