AppCompatActivityのフラグメントにイメージグリッドを作成するために作成した3つのJavaクラスをすべて貼り付けていますが、下に貼り付けたエラーが表示されています。フラグメントを使用したアンドロイドイメージグリッドビュー
私の主な活動:
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
};
}のGridViewと
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();
}
}
断片
私は、このエラーログを取得しています:
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)
'原因:java.lang.IllegalStateException:このアクティビティには、すでにウィンドウデコレーションによってアクションバーが提供されています。あなたのテーマでWindow.FEATURE_SUPPORT_ACTION_BARを要求せず、windowActionBarをfalseに設定して、代わりにツールバーを使用してください。 –
datを実行しようとしました、 –
このリンクを試してください http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor – Shubham