Android:应用程序在模拟器上运行,但在我的手机上崩溃

Posted

技术标签:

【中文标题】Android:应用程序在模拟器上运行,但在我的手机上崩溃【英文标题】:Android: App runs on emulator but it crashes on my mobile phone 【发布时间】:2017-05-12 22:07:42 【问题描述】:

我的应用在模拟器 Nexus 5(android 7.0,API 24)上运行,但是当我尝试使用“onClick”打开另一个活动时,它在我的手机(Samsung J5 Android 5.1.1)上崩溃。

这是崩溃的活动

public class CuboActivity extends AppCompatActivity 
    /**
     * The @link android.support.v4.view.PagerAdapter that will provide
     * fragments for each of the sections. We use a
     * @link FragmentPagerAdapter derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * @link android.support.v4.app.FragmentStatePagerAdapter.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The @link ViewPager that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cubo);
        Toast.makeText(getApplicationContext(),"Premi le foto per vederle ingradite oppure visita il menu per vederle una dopo l'altra",Toast.LENGTH_LONG).show();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    

    @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_cubo, 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.
        Toast.makeText(getApplicationContext(),"Per alcune immagini gira il telefono",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, FotoCuboActivity.class);
        startActivity(intent);
        return super.onOptionsItemSelected(item);
    

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment 
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() 
        

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) 
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        
            switch (getArguments().getInt(ARG_SECTION_NUMBER))
            
                case 1:
                    View rootView = inflater.inflate(R.layout.fragment_cubo, container, false);
                    return rootView;

                case 2:
                    View rootView2 = inflater.inflate(R.layout.fragment_cubo2, container, false);
                    return rootView2;

                case 3:
                    View rootView3 = inflater.inflate(R.layout.fragment_cubo3, container, false);
                    return rootView3;

                case 4:
                    View rootView4 = inflater.inflate(R.layout.fragment_cubo4, container, false);
                    return rootView4;

                default:
                    View rootView0 = inflater.inflate(R.layout.fragment_cubo, container, false);
                    return rootView0;
            
        
    

    /**
     * A @link FragmentPagerAdapter that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter 

        public SectionsPagerAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public Fragment getItem(int position) 
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        

        @Override
        public int getCount() 
            // Show 3 total pages.
            return 4;
        

        @Override
        public CharSequence getPageTitle(int position) 
            switch (position) 
                case 0:
                    return "CONSEGNA";
                case 1:
                    return "LAVORO";
                case 2:
                    return "RISULTATO";
                case 3:
                    return "RELAZIONE";
            
            return null;
        
    

    public void onClick(View arg0)
    
        switch(arg0.getId())
        
            case R.id.segaCircolare:
                Intent intent = new Intent(this,FragmentCuboActivity.class);
                startActivity(intent);
                break;
            case R.id.lavorazioneCubo:
                Intent intent2 = new Intent(this,FragmentCubo2Activity.class);
                startActivity(intent2);
                break;
            case R.id.prodottiFiniti:
                Toast.makeText(getApplicationContext(),"Gira il telefono",Toast.LENGTH_SHORT).show();
                Intent intent3 = new Intent(this,FragmentCubo3Activity.class);
                startActivity(intent3);
                break;
            case R.id.modelloCubo:
                Intent intent4 = new Intent(this,FragmentCubo4Activity.class);
                startActivity(intent4);
                break;
            case R.id.cuboAlto:
                Intent intent5 = new Intent(this,FragmentCubo5Activity.class);
                startActivity(intent5);
                break;
            case R.id.cuboFrontale:
                Intent intent6 = new Intent(this,FragmentCubo6Activity.class);
                startActivity(intent6);
                break;
            case R.id.cuboIntermedio:
                Intent intent7 = new Intent(this,FragmentCubo7Activity.class);
                startActivity(intent7);
                break;
            case R.id.bottoneRelazione:
                Uri uri = Uri.parse("https://docs.google.com/document/d/1J4KJJRzSnY5PSdQYoh6hrTKarl0ID0dtX8TqLga6-dY/edit?usp=sharing");
                Intent intent0 = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent0);
                break;
        
    

这是这个活动的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:text="@string/Procedimento1"
        android:layout_
        android:layout_
        android:id="@+id/textView14"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:gravity="center" />

    <ImageView
        android:layout_
        android:layout_
        android:background="@drawable/lavorazionecubo"
        android:id="@+id/lavorazioneCubo"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:contentDescription="@string/Vuoto"
        android:onClick="onClick"/>

    <TextView
        android:text="@string/Procedimento2"
        android:layout_
        android:layout_
        android:id="@+id/textView15"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:gravity="center" />

    <RelativeLayout
        android:orientation="horizontal"
        android:layout_
        android:layout_
        android:layout_marginTop="15dp">

        <ImageView
            android:layout_
            android:layout_
            android:background="@drawable/modellocubo"
            android:id="@+id/modelloCubo"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="40dp"
            android:layout_marginEnd="40dp"
            android:contentDescription="@string/Vuoto"
            android:onClick="onClick"/>

        <ImageView
            android:layout_
            android:layout_
            android:background="@drawable/prodottifiniti"
            android:id="@+id/prodottiFiniti"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:contentDescription="@string/Vuoto"
            android:onClick="onClick" />

    </RelativeLayout>
</LinearLayout>

这是上一个活动因崩溃而无法打开的活动之一

public class FragmentCubo2Activity extends AppCompatActivity 

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

这是它的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:background="@drawable/lavorazionecubo">
</LinearLayout>

这是日志猫

12-28 15:46:49.267 2751-2751/? I/art: Not late-enabling -Xcheck:jni (already on)
12-28 15:46:49.267 2751-2751/? W/art: Unexpected CPU variant for X86 using defaults: x86
12-28 15:46:49.326 2751-2751/com.example.utente.progettigscorsipa W/System: ClassLoader referenced unknown path: /data/app/com.example.utente.progettigscorsipa-2/lib/x86
12-28 15:46:49.332 2751-2751/com.example.utente.progettigscorsipa I/InstantRun: Instant Run Runtime started. Android package is com.example.utente.progettigscorsipa, real application class is null.
12-28 15:46:50.278 2751-2751/com.example.utente.progettigscorsipa W/System: ClassLoader referenced unknown path: /data/app/com.example.utente.progettigscorsipa-2/lib/x86
12-28 15:46:50.546 2751-2751/com.example.utente.progettigscorsipa W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-28 15:46:50.793 2751-2780/com.example.utente.progettigscorsipa I/OpenGLRenderer: Initialized EGL, version 1.4
12-28 15:46:50.793 2751-2780/com.example.utente.progettigscorsipa D/OpenGLRenderer: Swap behavior 1
12-28 15:46:50.948 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:46:50.948 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8e7ce4a0, error=EGL_BAD_MATCH
12-28 15:46:50.998 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:46:50.998 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8e7ce4e0, error=EGL_BAD_MATCH
12-28 15:49:16.811 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:16.811 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8edc9100, error=EGL_BAD_MATCH
12-28 15:49:19.918 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:19.918 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ce3cae0, error=EGL_BAD_MATCH
12-28 15:49:19.993 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:19.993 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9b091060, error=EGL_BAD_MATCH
12-28 15:49:20.017 2751-2751/com.example.utente.progettigscorsipa I/Choreographer: Skipped 55 frames!  The application may be doing too much work on its main thread.
12-28 15:49:25.376 2751-2751/com.example.utente.progettigscorsipa I/Choreographer: Skipped 78 frames!  The application may be doing too much work on its main thread.
12-28 15:49:26.001 2751-2751/com.example.utente.progettigscorsipa I/Choreographer: Skipped 36 frames!  The application may be doing too much work on its main thread.
12-28 15:49:26.598 2751-2751/com.example.utente.progettigscorsipa I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.
12-28 15:49:29.638 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:29.638 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ce3cac0, error=EGL_BAD_MATCH
12-28 15:49:32.900 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:32.900 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ce3cae0, error=EGL_BAD_MATCH
12-28 15:49:34.947 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:34.947 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8baf1c00, error=EGL_BAD_MATCH
12-28 15:49:35.386 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:35.386 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ce3c380, error=EGL_BAD_MATCH
12-28 15:49:38.009 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:38.009 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8baf1c00, error=EGL_BAD_MATCH
12-28 15:49:40.678 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:40.678 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ce3cae0, error=EGL_BAD_MATCH
12-28 15:49:43.712 2751-2780/com.example.utente.progettigscorsipa E/EGL_emulation: tid 2780: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
12-28 15:49:43.713 2751-2780/com.example.utente.progettigscorsipa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8baf1c00, error=EGL_BAD_MATCH

【问题讨论】:

拥有 logggggcatttt ,别以为我很喜欢ee aaa zooombieee @PavneetSingh 是 Logcat 事件日志吗? yuuup , logggcat @PavneetSingh 我发得好吗? @Curio 您发布的日志不是我们想要的。崩溃发生时应该有清晰的 Java 异常堆栈跟踪。请编辑您的帖子并将其包含在内。 【参考方案1】:

将您的 Android SDK 更新为您的 android 手机版本,并在 build.gradle 文件中定义您手机的最低和最高 sdk 版本。

android 
 ...
defaultConfig 
...
minSdkVersion 14
targetSdkVersion 24

productFlavors 
  main 
  ...
 
 afterLollipop 
   ...
  minSdkVersion 21



【讨论】:

@sunnythesine 是否必须将 targetSDKversion 更改为我的手机版本? 是的,您必须定义您的应用可以运行的最低版本。根据您的帖子,您的模拟器是 Nexus 5(Android 7.0,API 24),而您的手机是(Samsung J5 Android 5.1.1),两者都有不同的 API 版本。 现在15了,我有一个移动的20 sdk,要不要把15改成20? 所以最小值应该是 15,最大值应该是 API 级别 25,根据你的 logcat 日志,当你在手机上运行它时,应用程序的目录丢失了,检查权限和存储空间。 那我需要释放手机空间吗?【参考方案2】:

我的问题解决了,是手机问题,内存不够

【讨论】:

以上是关于Android:应用程序在模拟器上运行,但在我的手机上崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Godot,为啥我的 android 导出找不到/打开文件?

我的 Android 应用程序在模拟器上运行,但无法在我的 android 设备上运行

应用程序在模拟器中运行,但在我的 iPhone 中关闭

Xamarin表单,在Visual Studio Android Emulator上运行的应用程序但在真实设备上崩溃

iOS:音频录制设置在模拟器中失败,但在我的设备上没有?

我无法在Android模拟器上打开文件