Fatal Exception: main java.lang.RuntimeException:

2019-08-07 07:38发布

问题:

I added scrollview in my activity_main.xml and its not working anymore. Before scrollview it did not work on Samsung s3,s4. But it worked on other phones and tablets.

02-15 05:37:35.700: D/AndroidRuntime(936): Shutting down VM
02-15 05:37:35.700: W/dalvikvm(936): threadid=1: thread exiting with uncaught exception (group=0xb2a78ba8)
02-15 05:37:35.780: E/AndroidRuntime(936): FATAL EXCEPTION: main
02-15 05:37:35.780: E/AndroidRuntime(936): Process: com.oxygen.hayvansesleri, PID: 936
02-15 05:37:35.780: E/AndroidRuntime(936): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oxygen.hayvansesleri/com.oxygen.hayvansesleri.screencat}: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.os.Looper.loop(Looper.java:136)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-15 05:37:35.780: E/AndroidRuntime(936):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 05:37:35.780: E/AndroidRuntime(936):  at java.lang.reflect.Method.invoke(Method.java:515)
02-15 05:37:35.780: E/AndroidRuntime(936):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-15 05:37:35.780: E/AndroidRuntime(936):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-15 05:37:35.780: E/AndroidRuntime(936):  at dalvik.system.NativeStart.main(Native Method)
02-15 05:37:35.780: E/AndroidRuntime(936): Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
02-15 05:37:35.780: E/AndroidRuntime(936):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-15 05:37:35.780: E/AndroidRuntime(936):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.Activity.setContentView(Activity.java:1929)
02-15 05:37:35.780: E/AndroidRuntime(936):  at com.oxygen.hayvansesleri.screencat.onCreate(screencat.java:18)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.Activity.performCreate(Activity.java:5231)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-15 05:37:35.780: E/AndroidRuntime(936):  ... 11 more
02-15 05:37:35.780: E/AndroidRuntime(936): Caused by: java.lang.reflect.InvocationTargetException
02-15 05:37:35.780: E/AndroidRuntime(936):  at java.lang.reflect.Constructor.constructNative(Native Method)
02-15 05:37:35.780: E/AndroidRuntime(936):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
02-15 05:37:35.780: E/AndroidRuntime(936):  ... 24 more
02-15 05:37:35.780: E/AndroidRuntime(936): Caused by: java.lang.OutOfMemoryError
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.content.res.Resources.loadDrawable(Resources.java:2110)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.widget.ImageView.<init>(ImageView.java:129)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.widget.ImageButton.<init>(ImageButton.java:87)
02-15 05:37:35.780: E/AndroidRuntime(936):  at android.widget.ImageButton.<init>(ImageButton.java:83)
02-15 05:37:35.780: E/AndroidRuntime(936):  ... 27 more
02-15 05:37:42.930: W/Ads(936): There was a problem getting an ad response. ErrorCode: 0
02-15 05:37:47.120: I/Process(936): Sending signal. PID: 936 SIG: 9

public class MainActivity extends Activity {

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

        AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        final MediaPlayer click = MediaPlayer.create(MainActivity.this,
                R.raw.click);

        ImageButton catbutton = (ImageButton) findViewById(R.id.catbutton);
        catbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screencat.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton dogbutton = (ImageButton) findViewById(R.id.dogbutton);
        dogbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screendog.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton bearbutton = (ImageButton) findViewById(R.id.bearbutton);
        bearbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screenbear.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton cowbutton = (ImageButton) findViewById(R.id.cowbutton);
        cowbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screencow.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton duckbutton = (ImageButton) findViewById(R.id.duckbutton);
        duckbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screenduck.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        ImageButton hindibutton = (ImageButton) findViewById(R.id.hindibutton);
        hindibutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screenhindi.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        ImageButton lionbutton = (ImageButton) findViewById(R.id.lionbutton);
        lionbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screenlion.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        ImageButton tigerbutton = (ImageButton) findViewById(R.id.tigerbutton);
        tigerbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screentiger.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        ImageButton donkeybutton = (ImageButton) findViewById(R.id.donkeybutton);
        donkeybutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this,
                        screendonkey.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton horsebutton = (ImageButton) findViewById(R.id.horsebutton);
        horsebutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screenhorse.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        ImageButton goatbutton = (ImageButton) findViewById(R.id.goatbutton);
        goatbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                click.start();

                Intent intent = new Intent(MainActivity.this, screengoat.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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;
        }
        return super.onOptionsItemSelected(item);
    }
}

回答1:

You've got this error in setContentView(R.layout.activity_main); and its because of memory problems. It seems you've used a big image. You must scale down your image.



回答2:

Change

setContentView(R.layout.activity_main);

to

View _mw = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(_mw);


回答3:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.kushagra.dynamiclist, PID: 2456 android.view.InflateException: Binary XML file line #9: Error inflating class unknown

Got the same issue, wasted my day trying to find the problem. My app worked well in both emulator and real device for phones but crashed while using tablets or the ones with larger screen. I also tried increasing my memory heap but that didn't help. The listview I had been using had android:background="@drawable/edittextborder" which was the source of the crash. Didn't know why or how but removing that solved the issue for me.

<LinearLayout
        android:background="@drawable/edittextborder"
        android:layout_margin="5dp"
        android:id="@+id/allListData"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView

            android:id="@+id/logo"
            android:layout_width="30dp"
            android:layout_height="30dp" />

        <TextView

            android:paddingLeft="10dp"
            android:id="@+id/listName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>

    </LinearLayout>