当我点击 ImageView 时 App 停止

when i click on ImageView the App stop

在我的应用程序中,我尝试添加一些 ImageView (play2_one and play2_two),然后在单击 ImageView 后导航到另一个 Activity。它在 NEXUS S(API 25) 中正常工作,但在 NOTE 3、NEXUS 5(API 24).[=29= 中不正常]

可能是什么问题?请指导:

这是我的 logcat:

android.view.InflateException: Binary XML file line #71: Error inflating class at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2578) at android.app.ActivityThread.access0(ActivityThread.java:170) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5727) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #71: Error inflating class at android.view.LayoutInflater.createView(LayoutInflater.java:626) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at android.view.LayoutInflater.inflate(LayoutInflater.java:354) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:352) at android.app.Activity.setContentView(Activity.java:2058) at com.example.compassapp.MainActivity.onCreate(MainActivity.java:32) at android.app.Activity.performCreate(Activity.java:5581) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2483) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2578)  at android.app.ActivityThread.access0(ActivityThread.java:170)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)

这是我的 xml 代码:

<LinearLayout
    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"
    android:background="@drawable/bitmap">

    <LinearLayout
        android:layout_margin="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">

        <ImageView
            android:id="@+id/play1_one"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:src="@drawable/four" />

        <TextView
            android:id="@+id/txt1"
            android:textSize="25sp"
            android:gravity="center"
            android:textColor="#ffffff"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            />

        <ImageView
            android:id="@+id/play1_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:src="@drawable/three" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/qiblaFrameLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="15dp">

            <ImageView
                android:id="@+id/frameImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/amer" />

            <ImageView
                android:id="@+id/compassImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/nnn" />

        </FrameLayout>
    </FrameLayout>


</LinearLayout>

和这个主要活动:

public class MainActivity extends Activity implements SensorEventListener {

    // define the display assembly compass picture
    private ImageView image1;

    // record the compass picture angle turned
    private float currentDegree = 0f;

    // device sensor manager
    private SensorManager mSensorManager;

    private TextView tvHeading1;

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

        // our compass image
        image1 = (ImageView) findViewById(R.id.compassImage);

        // TextView that will tell the user what degree is he heading
        tvHeading1 = (TextView) findViewById(R.id.txt3);
        // initialize your android device sensor capabilities
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        ImageView imageView1 = (ImageView) findViewById(R.id.play1_one);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(MainActivity.this, CompassThree.class);
                startActivity(intent1);
            }
        });
        ImageView imageView2 = (ImageView) findViewById(R.id.play1_two);
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(MainActivity.this, Compasstwo.class);
                startActivity(intent1);
            }
        });

        imageView1.setClickable(true);
        imageView2.setClickable(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // for the system's orientation sensor registered listeners
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // to stop the listener and save battery
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {


    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // not in use
    }


}

由于您在布局中添加了如此多的可绘制对象,许多设备上的内存可能存在问题,正如您所说,它在 NEXUS S 中正常工作(API 25 ) 但不在 注 3,NEXUS 5(API 24) 中。

如果 Id 或其他问题存在问题,那么它将无法在任何设备上运行,因此很可能是内存问题。

您可以增加内存堆大小,请参阅 this answer

另外,参考this answer那里也描述了类似的问题。