为什么我的应用程序无法在模拟器中运行?

时间:2017-11-05 15:52:06

标签: java android android-studio android-emulator logcat

每当我尝试在Android模拟器中运行我的应用程序时,我的应用程序都不会显示。我得到了这个error,但我很难解读它。我该如何解决这个问题,以便在模拟器中看到我的应用程序? 我认为它与我的一行代码混乱有关,但我无法找到它。

这是MainActivity.java

import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements Place.OnFragmentInteractionListener, Profile.OnFragmentInteractionListener, Take.OnFragmentInteractionListener {

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

        TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText("Place"));
        tabLayout.addTab(tabLayout.newTab().setText("Profile"));
        tabLayout.addTab(tabLayout.newTab().setText("Take"));

        final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

这是PagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class PagerAdapter extends FragmentStatePagerAdapter {

    int mNoOfTabs;

    public PagerAdapter(FragmentManager fm, int NumberOfTabs) {
        super(fm);
        this.mNoOfTabs = NumberOfTabs; // set global number of tabs to local number of tabs
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
            case 0:
                Place place = new Place();
                return place;
            case 1:
                Profile profile = new Profile();
                return profile;
            case 2:
                Take take = new Take();
                return take;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 0;
    }
}

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:minHeight="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SlidingTabsBar"
            android:textSize="20dp"/>


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    </android.support.design.widget.TabLayout>

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

</LinearLayout>

这是error

11/05 10:56:42: Launching app
$ adb shell am start -n "com.navi.navi/com.navi.navi.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 4085 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.navi.navi, PID: 4085
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.navi.navi/com.navi.navi.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.widget.Toolbar
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                   Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.v7.widget.Toolbar
                   Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.v7.widget.Toolbar
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Constructor.newInstance0(Native Method)
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
                      at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at com.navi.navi.MainActivity.onCreate(MainActivity.java:15)
                      at android.app.Activity.performCreate(Activity.java:6975)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                   Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 37 to dimension: type=0x1
                      at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
                      at android.view.View.<init>(View.java:4998)
                      at android.view.ViewGroup.<init>(ViewGroup.java:597)
                      at android.view.ViewGroup.<init>(ViewGroup.java:593)
                      at android.support.v7.widget.Toolbar.<init>(Toolbar.java:231)
                      at android.support.v7.widget.Toolbar.<init>(Toolbar.java:227)
                      at java.lang.reflect.Constructor.newInstance0(Native Method) 
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
                      at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) 
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                      at com.navi.navi.MainActivity.onCreate(MainActivity.java:15) 
                      at android.app.Activity.performCreate(Activity.java:6975) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6541) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Application terminated.

1 个答案:

答案 0 :(得分:0)

在布局XML Toolbar元素中:

android:minHeight="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

样式引用不是可以转换为维度的东西,因此是异常

Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 37 to dimension: type=0x1
 at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
 ...
 at android.support.v7.widget.Toolbar.<init>(Toolbar.java:231)
 ...
 at com.navi.navi.MainActivity.onCreate(MainActivity.java:15) 

我离开了堆栈跟踪的最基本部分。

如果您尝试引用样式资源,请使用style属性执行此操作。如果您尝试设置android:minHeight,请使用维度值,例如40dp@dimen/something引用。