如何修复Android应用崩溃问题?

时间:2017-12-28 18:07:09

标签: java android xml

我在android studio上的项目在少数手机和模拟器上正常运行但在许多手机上都崩溃了。我有一个Main_Activity,点击登录进入第二个活动。我正在与android manifest共享这两个活动的java文件和xml文件。

主要活动Java文件

package club.pithynews.iitbhilai_pithy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private Button login_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        LoginButton();
    }

    public void LoginButton() {
        username = (EditText) findViewById(R.id.editText);
        password = (EditText) findViewById(R.id.editText2);
        login_button = (Button) findViewById(R.id.button);

        login_button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ((username.getText().toString().equals("user"))&&(password.getText().toString().equals("pass"))) {
                            Intent intent = new Intent("club.pithynews.iitbhilai_pithy.User");
                            startActivity(intent);
                        }
                    }
                }
        );
    }
}

主要活动XML文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  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"
    android:background="@drawable/back_login"
    tools:context="club.pithynews.iitbhilai_pithy.MainActivity">



    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="#11000000"
        android:ems="10"
        android:hint="@string/user_name"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.78"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.53" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="#11000000"
        android:ems="10"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.78"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.64" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        android:maxHeight="30dp"
        android:maxWidth="30dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.05"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.53"
        app:srcCompat="@drawable/user1"
        android:contentDescription="@string/todo" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"
        android:adjustViewBounds="true"
        android:maxHeight="30dp"
        android:maxWidth="30dp"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.06"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.64"
        app:srcCompat="@drawable/lock1"
        android:contentDescription="@string/todo" />

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="40dp"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"

        android:layout_marginTop="8dp"
        android:background="@drawable/back_login"
        android:text="@string/login"
        android:textSize="28sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.79" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="233dp"
        android:layout_height="227dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="20dp"


        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.43"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.057"
        app:srcCompat="@drawable/logoiit"
        android:contentDescription="@string/todo" />



</android.support.constraint.ConstraintLayout >

第二个活动Java文件

package club.pithynews.iitbhilai_pithy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class User extends AppCompatActivity {

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

第二个活动XML文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/white"
    tools:context="club.pithynews.iitbhilai_pithy.User">

    <ImageButton
        android:id="@+id/imageButton_emergency3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="80dp"
        android:maxHeight="100dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="270dp"
        android:layout_marginTop="350dp"
        android:src="@drawable/emergency3"
        android:background="@null"
        android:layout_marginStart="270dp"
        android:contentDescription="@string/todo13" />

    <ImageButton
        android:id="@+id/imageButton_feedback1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="125dp"
        android:maxHeight="100dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="265dp"
        android:layout_marginTop="445dp"
        android:src="@drawable/feedback1"
        android:background="@null"
        android:layout_marginStart="265dp"
        android:contentDescription="@string/todo12" />

    <ImageButton
        android:id="@+id/imageButton_aboutus1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="125dp"
        android:maxHeight="500dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="125dp"
        android:layout_marginTop="455dp"
        android:src="@drawable/aboutus1"
        android:background="@null"
        android:layout_marginStart="125dp"
        android:contentDescription="@string/todo11" />

    <ImageButton
        android:id="@+id/imageButton_campusmap1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="125dp"
        android:maxHeight="80dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="455dp"
        android:src="@drawable/campusmap1"
        android:background="@null"
        android:layout_marginStart="20dp"
        android:contentDescription="@string/todo10" />
    <ImageButton
        android:id="@+id/imageButton_adminstrators"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="100dp"
        android:maxHeight="500dp"
        android:scaleType="fitCenter"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="350dp"
        android:src="@drawable/adminstrators"
        android:background="@null"
        android:layout_marginStart="140dp"
        android:contentDescription="@string/todo9" />

    <ImageButton
        android:id="@+id/imageButton_fnr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="350dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:contentDescription="@string/fee_and_registration"
        android:maxHeight="200dp"
        android:maxWidth="75dp"
        android:scaleType="fitCenter"
        android:src="@drawable/fnr"

        android:layout_marginStart="25dp" />

    <ImageButton
        android:id="@+id/imageButton_clubs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="265dp"
        android:layout_marginTop="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/clubs"
        android:layout_marginStart="265dp"
        android:contentDescription="@string/todo8" />

    <ImageButton
        android:id="@+id/imageButton_studdata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="135dp"
        android:layout_marginTop="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/studdata"
        android:layout_marginStart="135dp"
        android:contentDescription="@string/todo7" />

    <ImageButton
        android:id="@+id/imageButton_bus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="220dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/bus"
        android:layout_marginStart="15dp"
        android:contentDescription="@string/todo6" />

    <ImageButton
        android:id="@+id/imageButton_mess"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="265dp"
        android:layout_marginTop="100dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/mess"
        android:layout_marginStart="265dp"
        android:contentDescription="@string/todo5" />

    <ImageButton
        android:id="@+id/imageButton_faculty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="135dp"
        android:layout_marginTop="100dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/faculty"
        android:layout_marginStart="135dp"
        android:contentDescription="@string/todo4" />

    <ImageButton
        android:id="@+id/imageButton_academics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="100dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/academics"
        android:layout_marginStart="15dp"
        android:contentDescription="@string/todo3" />

    <ImageButton
        android:id="@+id/imageButton_newsletter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="265dp"
        android:layout_marginTop="0dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/news"
        android:layout_marginStart="265dp"
        android:contentDescription="@string/todo" />

    <ImageButton
        android:id="@+id/imageButton_events"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="135dp"
        android:layout_marginTop="10dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/events"
        android:layout_marginStart="135dp"
        android:contentDescription="@string/todo1" />

    <ImageButton
        android:id="@+id/imageButton_today"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="0dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:maxHeight="500dp"
        android:maxWidth="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/today"
        android:layout_marginStart="15dp"
        android:contentDescription="@string/todo2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignEnd="@+id/imageButton_emergency3"
        android:layout_alignRight="@+id/imageButton_emergency3"
        android:text="@string/textview5"
        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible"
        tools:text="   Emergency  " />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignEnd="@+id/imageButton_adminstrators"
        android:layout_alignRight="@+id/imageButton_adminstrators"
        android:text="@string/administrators"
        android:textColor="@color/colorPrimaryDark" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton_bus"
        android:layout_alignStart="@+id/imageButton_bus"
        android:layout_below="@+id/imageButton_fnr"
        android:text="@string/textview4"
        android:textColor="@color/colorPrimaryDark"
        tools:text=" Fees and\n Registration" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignStart="@+id/textView3"
        android:layout_below="@+id/imageButton_campusmap1"
        android:text="@string/textview3"
        tools:text=" Campus Map"
        android:textColor="@color/colorPrimaryDark"/>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton_adminstrators"
        android:layout_alignStart="@+id/imageButton_adminstrators"
        android:layout_below="@+id/imageButton_campusmap1"
        android:text="@string/textview2"
        tools:text="      About Us"
        android:textColor="@color/colorPrimaryDark"/>

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton_feedback1"
        android:layout_alignStart="@+id/imageButton_feedback1"
        android:layout_alignTop="@+id/textView5"
        android:text="@string/textview1"
        tools:text="     Feedback"
        android:textColor="@color/colorPrimaryDark"/>
</RelativeLayout> 

Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="club.pithynews.iitbhilai_pithy">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".User">
            <intent-filter>
                <action android:name="club.pithynews.iitbhilai_pithy.User" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

</resources>

应用崩溃时的调试报告

W/System: ClassLoader referenced unknown path: /data/app/club.pithynews.iitbhilai_pithy-1/lib/arm
I/InstantRun: starting instant run server: is main process
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
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
W/ResourceType: Failure getting entry for 0x7f06005a (t=5 e=90) (error -75)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: club.pithynews.iitbhilai_pithy, PID: 16468
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{club.pithynews.iitbhilai_pithy/club.pithynews.iitbhilai_pithy.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                   Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.constraint.ConstraintLayout
                      at android.app.Activity.performCreate(Activity.java:6904) 
                      at a
                      at android.app.ActivityThread.main(ActivityThread.java:7331) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                   Caused by: android.content.res.Resources$NotFoundException: Resource "club.pithynews.iitbhilai_pithy:drawable/back_login" (7f06005a) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f06005a a=-1 r=0x7f06005a}
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:7331) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

3 个答案:

答案 0 :(得分:1)

错误是自我解释的。您正在尝试加载太大的图片。

Canvas: trying to draw too large(111300000bytes) bitmap.

您在ImageButtonImageView上设置的一个可绘制来源太大了。你可以:

  • 手动缩放图像
  • 在代码中执行缩放并在之后设置图像

答案 1 :(得分:0)

java.lang.RuntimeException: Canvas: trying to draw too large(111300000bytes) bitmap.

如您所见,您正在尝试显示太大的图像。检查包含的位图资源的大小。

答案 2 :(得分:0)

您正在显示大小 111.3 mb 的图片,该图片太大而无法处理您需要在显示之前压缩和缩放到某个较小的版本。 在这里你可以扩展你的形象。

Bitmap scaled = Bitmap.createScaledBitmap(originalBitmap, 120, 120, false);

更多here 并使用

进行压缩
ByteArrayOutputStream out = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);

查看更多here