崩溃可能是由于活动布局不好造成的

时间:2017-12-26 14:46:57

标签: java android crash

我一直在开发一个应用程序,它一直运行良好,直到我在该活动的相应Java文件中添加了一个LinearLayout(id ='toolbar')和一些函数。这是我的代码:

的AndroidManifest.xml

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

<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=".EditPhotoActivity"
        android:parentActivityName=".MainActivity">
        <meta-data android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>

</manifest>

MainActivty运作良好,我不认为问题在其中。如果您要我添加其代码,请告诉我。

EditPhotoActivity.java

package com.example.com.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.IOException;

public class EditPhotoActivity extends AppCompatActivity {

    public ImageView image_view = findViewById(R.id.image_display) ;
    public boolean toolbar_is_open = false ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_photo);
        ImageView imageView = findViewById(R.id.image_display);
        Intent intent = getIntent();
        Uri content;
        if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Uri")) {
            Bundle bundle = intent.getExtras();
            if(bundle != null) {
                content = (Uri) bundle.get(MainActivity.IMAGE_KEY);
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), content);
                    imageView.setImageBitmap(bitmap);
                } catch(IOException e) {
                    e.printStackTrace();
                    Toast.makeText(this, "Failed to fetch image data!", Toast.LENGTH_LONG).show() ;
                }
            }
        } else if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Bundle")) {
            Bundle bundle = intent.getBundleExtra(MainActivity.IMAGE_KEY);
            Bitmap bitmap = (Bitmap)bundle.get("data");
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.publish_menu, menu);
        return true ;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.share_button:
                // Save the photo
            break;
            case R.id.save_button:
                // Open Share Photo Activity
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setToolbarState(boolean open) {
        LinearLayout toolbar = findViewById(R.id.toolbar);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)toolbar.getLayoutParams();
        params.height = open? 50 : 0;
        toolbar.setLayoutParams(params);
        toolbar_is_open = open ;
    }

    public void openRotateToolbar(View view) {
        Button right = new Button(this), left = new Button(this);
        right.setText(R.string.right); left.setText(R.string.left);
        right.setBackgroundResource(R.drawable.custom_button);
        left.setBackgroundResource(R.drawable.custom_button);
        right.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                image_view.setRotation(90f);
            }
        });
        left.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                image_view.setRotation(-90f);
            }
        });
        if(!toolbar_is_open)
          setToolbarState(true);
    }

    public void closeToolbar(View view) {
        if(toolbar_is_open)
          setToolbarState(false);
    }
}

activity_edit_photo.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"
    tools:context="com.example.com.myapplication.EditPhotoActivity"
    tools:layout_editor_absoluteY="81dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/image_display"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:adjustViewBounds="true"
                android:background="@color/black"
                android:padding="0dp"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toTopOf="@+id/toolbar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_launcher_background"
                tools:ignore="contentDescription" />

            <LinearLayout
                android:id="@+id/toolbar"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="#111"
                android:orientation="horizontal"
                app:layout_constraintBottom_toTopOf="@+id/scrollView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent">

                <Button
                    android:id="@+id/button4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/close"
                    android:background="@drawable/custom_button"
                    android:textColor="#fff"
                    android:onClick="closeToolbar" />
            </LinearLayout>

            <HorizontalScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:background="@color/darkGrey"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:text="@string/add_sticker"
                        android:textColor="#fff"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:layout_marginTop="-15dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/create_sticker"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/cut_sticker"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/apply_filter"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/rotate"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle"
                        android:onClick="openRotateToolbar" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/flip"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:background="@drawable/custom_button"
                        android:gravity="center"
                        android:text="@string/crop"
                        android:textSize="12sp"
                        tools:ignore="ButtonStyle" />


                </LinearLayout>

            </HorizontalScrollView>

        </android.support.constraint.ConstraintLayout>

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

我没有收到任何错误,但当应用程序转换为EditPhoto活动时,它崩溃了。如果您需要我提供任何其他信息,请与我们联系。

注意:我无法发布logcat,因为我没有使用ADB,而且我没有使用全是我的计算机(这是我的家人的)所以我不想启用x-在BIOS中vt,所以我也无法使用模拟器。为了测试我的应用程序,我构建了apk然后我将其安装在手机上。

1 个答案:

答案 0 :(得分:1)

我发现了自己的错误。对于遇到同样问题的人,我会在这里回答这个问题。

该行:

public ImageView image_view = findViewById(R.id.image_display) ;

导致NullPointerException,因为在应用程序打开活动时,它还没有所有视图和ID。它仅在调用onCreate时获取它们,因此我将该行替换为

public ImageView image_view ;
在我添加的onCreatesuper.onCreate(savedInstanceState)来电后,

setContentView(R.layout.activity_edit_photo)内部

image_view = findViewById(R.id.image_display) ;