gpt4 book ai didi

java - 在抽屉导航中添加 Google map Activity 时出错( Material 设计)

转载 作者:行者123 更新时间:2023-12-01 09:45:35 26 4
gpt4 key购买 nike

我在实现 Material Design 的 Android 应用程序中使用 Google map Activity ,当我尝试在抽屉导航中添加 Google map Activity 时,应用程序显示错误“不兼容类型 - 必需:android.support.v4。应用程序 fragment ”。这是我的 mainactivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import info.androidhive.materialdesign.R;


public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private WebView myWebView;
private static String TAG = MainActivity.class.getSimpleName();


private Toolbar mToolbar;
private FragmentDrawer drawerFragment;



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

mToolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);


drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);




// display the first navigation drawer view on app launch
displayView(0);
}


@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();


if (id == R.id.action_settings) {
return true;
}

if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}

private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;

case 1:
fragment = new FriendsFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.title_messages);
break;
case 3:
fragment = new ContactFragment();
title = getString(R.string.title_contact);
break;
case 4:
fragment=new HospitalMapsActivity();
title = "Detect Hospital";
break;
default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();

// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
}

最佳答案

我还没有看到你的具体实现,但我认为这个类中的一个(或全部)是android.app.Fragment而不是android.support.v4.app。 fragment :HomeFragmentFriendsFragmentMessagesFragmentContactFragment

要解决您的问题,您的 fragment 必须扩展 android.support.v4.app.Fragment

关于java - 在抽屉导航中添加 Google map Activity 时出错( Material 设计),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com