应用程序无法启动但代码中没有错误

时间:2016-03-25 11:14:41

标签: android google-maps android-fragments supportmapfragment

我有一个标签式布局应用程序,第三个标签已实现谷歌地图,没有代码错误但是当我在模拟器中启动它甚至没有启动它只是声明它已被停止

这是我的主要活动类

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

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Home", "Shouts", "Maps", };
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new HomeFragment();
            case 1:
                return new ShoutsFragment();
            case 2:
                return new MapFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.mapfragment, null);
        TextView tv = (TextView) tab.findViewById(R.id.map);
        tv.setText(tabTitles[position]);
        return tab;
    }

    }

这是我的活动主xml

<RelativeLayout
android:id="@+id/main_layout"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    app:tabMode="fixed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    app:tabTextColor="#d3d3d3"
    app:tabSelectedTextColor="#ffffff"
    app:tabIndicatorColor="#ff00ff"
    android:minHeight="?attr/actionBarSize"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

这是我的地图片段类

public class MapFragment extends Fragment {

MapView mMapView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // inflate and return the layout
    View v = inflater.inflate(R.layout.mapfragment, container,
            false);
    mMapView = (MapView) v.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync((OnMapReadyCallback) this);
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));



    // Perform any camera updates here
    return v;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}
}

这是我的片段映射xml文件

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
xmlns:android="http://schemas.android.com/apk/res/android"
/>

这是我的家庭片段类

public class HomeFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment home.xml
    View view = inflater.inflate(R.layout.home_fragment, container, false);
    return view;

}
}

这是我的家庭片段xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/custom_text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="16dip"
    android:textColor="#ffffff"
    android:singleLine="true"
    />
</LinearLayout>

这是我的喊叫片段类

public class ShoutsFragment extends Fragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment home.xml
    View view = inflater.inflate(R.layout.shouts_fragment, container, false);
    return view;

}
}

这是我的shouts xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/custom_text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="16dip"
    android:textColor="#ffffff"
    android:singleLine="true"
    />
 </LinearLayout>

这是我的logcat

03-25 11:06:59.012 3016-3016/com.example.hp_user.dn_tab1 I/art: Not late-enabling -Xcheck:jni (already on)
03-25 11:06:59.483 3016-3016/com.example.hp_user.dn_tab1 W/System:     ClassLoader referenced unknown path: /data/app/com.example.hp_user.dn_tab1-    2/lib/x86
03-25 11:06:59.669 3016-3016/com.example.hp_user.dn_tab1 I/GMPM: App                 measurement is starting up, version: 8487
03-25 11:06:59.669 3016-3016/com.example.hp_user.dn_tab1 I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
03-25 11:06:59.759 3016-3016/com.example.hp_user.dn_tab1 E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
03-25 11:06:59.813 3016-3016/com.example.hp_user.dn_tab1 E/GMPM: Scheduler not set. Not logging error/warn.
03-25 11:07:00.045 3016-3040/com.example.hp_user.dn_tab1 E/GMPM: Uploading is not possible. App measurement disabled
03-25 11:07:00.379 3016-3016/com.example.hp_user.dn_tab1 D/AndroidRuntime: Shutting down VM
03-25 11:07:00.381 3016-3016/com.example.hp_user.dn_tab1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.hp_user.dn_tab1, PID: 3016
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp_user.dn_tab1/com.example.hp_user.dn_tab1.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:197)
                                                                           at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                           at com.example.hp_user.dn_tab1.MainActivity.onCreate(MainActivity.java:27)
                                                                           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) 
03-25 11:07:08.761 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 82.187ms
03-25 11:07:08.772 3016-3026/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 9.588ms
03-25 11:07:20.033 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 32.982ms
03-25 11:07:24.581 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 91.462ms
03-25 11:07:32.640 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 159.934ms
03-25 11:07:42.053 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 50.217ms
03-25 11:07:47.886 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 10.813ms
03-25 11:07:50.622 3016-3022/com.example.hp_user.dn_tab1 W/art: Suspending all threads took: 331.902ms

1 个答案:

答案 0 :(得分:0)

您的logcat输出确切地告诉您发生了什么以及如何解决它:

  

此活动已有   窗户装饰提供的动作栏。不要求   Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false   您的主题是使用工具栏。

我打赌主题设置:

  

使用工具栏替换ActionBar非常简单   并从确保活动延伸开始   ActionBarActivity。还要确保此活动的主题是   Theme.AppCompat.NoActionBar或的子元素   Theme.AppCompat.Light.NoActionBar。如果你没有使用   然后您可以添加以下主题.AppCompat变体   你的主题的行:

<item name="android:windowNoTitle">true</item> 
<item name="windowActionBar">false</item>

请参阅此处的完整文章:http://mrengineer13.com/how-to-use-toolbar-to-replace-your-actionbar/