我的意图不起作用,应用程序每次都崩溃

问题描述 投票:2回答:1

object name.Java

package randomexcessor.foutuneteller;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class objectName extends AppCompatActivity {

EditText objectInput;
FloatingActionButton actionButton;
TextView text;

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

// An intent received and hence been written bellow the Edit_text input to 
be shown as consideration.
    Intent toAgeInput = getIntent();
    final String name = toAgeInput.getStringExtra("users name");
    final String display = toAgeInput.getStringExtra("alphabet");

    TextView show = (TextView) findViewById(R.id.disclaimer);
    show.setText("Please type the name starting with the alphabet, Capital ' 
" + display+ " '");

    objectInput = (EditText) findViewById(R.id.inputName);
    actionButton = (FloatingActionButton) findViewById(R.id.outputButton);

    LayoutInflater inflater = getLayoutInflater();
    final View layout = inflater.inflate(R.layout.toast_layout,
            (ViewGroup) findViewById(R.id.custom_toast_container));



    actionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*condition layout which allows user to dial only an alphabet
             starting with random selected alphabet from baseInt activity 
   input.*/
            if (!objectInput.getText().toString().matches(display+ "[a-zA-
   Z]+"))
            {
                text = (TextView) layout.findViewById(R.id.errorReport);
                text.setText("You have to Start with 'Capital " + 
  display+"'.");

                Toast toast = new Toast(objectName.this);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
                toast.setView(layout);
                toast.show();
            }
            else if (!objectInput.getText().toString().matches("[a-zA-Z]+"))
            {
                text = (TextView) layout.findViewById(R.id.errorReport);
                text.setText("Your name must be in words");

                Toast toast = new Toast(objectName.this);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
                toast.setView(layout);
                toast.show();
            }
            else
            {
                Intent openAge = new Intent(objectName.this, 
 ageInput.class);
                String input = objectInput.getText().toString();
                openAge.putExtra("alphabet", display);//alphabet to use
                openAge.putExtra("users name", name);//name of the user.
                openAge.putExtra("belonging", input);//the object name 
 provided in this class.
                startActivity(openAge);//running activity.

            }
        }

    });

   }}

这个文件几乎工作得很好,但每当我点击下一个按钮时,它就会崩溃应用程序

activity_objectName.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/object1bg">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/appbar2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="45dp"
        app:srcCompat="@drawable/type15"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:id="@+id/appbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5sp"
        android:background="@drawable/edit_background"
        android:padding="2dp"
        android:weightSum="1">

        <EditText
            android:id="@+id/inputName"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/edit_text"
            android:ems="10"
            android:inputType="text"
            android:padding="10dp"
            android:textSize="18sp"
            android:layout_margin="3dp"
            android:layout_weight="1.02"
            android:hint="@string/Object"
            android:textColor="#A52A2A"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/outputButton"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:backgroundTint="#32CD32"
            app:elevation="2dp"
            app:fabSize="normal"
            app:srcCompat="@drawable/ic_keyboard_arrow_right_white_24dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appbar2"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/button"
        tools:ignore="UnknownIdInLayout">

        <TextView
            android:id="@+id/disclaimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />
    </LinearLayout>

</RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

主要问题在这里我无法访问此文件“ageInput.java”,

activity_ageInput.xml

package randomexcessor.foutuneteller;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ageInput extends AppCompatActivity {

TextView text;

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

    final EditText ageFind = (EditText) findViewById(R.id.inputAge);
    FloatingActionButton ageButton = (FloatingActionButton) 
findViewById(R.id.ageButton);

    LayoutInflater inflater = getLayoutInflater();
    final View layout = inflater.inflate(R.layout.toast_layout,
            (ViewGroup) findViewById(R.id.custom_toast_container));



    ageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ageFind.getText().toString().isEmpty())
            {

                text = (TextView) layout.findViewById(R.id.errorReport);
                text.setText("Please type your age or System error.");

                text = (TextView) layout.findViewById(R.id.errorReport);
                Toast toast = new Toast(ageInput.this);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
                toast.setView(layout);
                toast.show();
            }
            else if(ageFind.getText().length() > 2)
            {
                text = (TextView) layout.findViewById(R.id.errorReport);
                text.setText("Your age cannot be " + 
 ageFind.getText().toString() +"!!");

                text = (TextView) layout.findViewById(R.id.errorReport);
                Toast toast = new Toast(ageInput.this);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
                toast.setView(layout);
                toast.show();
            }
            else
            {
                Intent runResult = getIntent();
                final String alpha = runResult.getStringExtra("alphabet");
                final String name = runResult.getStringExtra("users name");
                final String belong = runResult.getStringExtra("belonging");

                Intent finalPage = new Intent(ageInput.this, result.class);
                finalPage.putExtra("alphabet", alpha);
                finalPage.putExtra("users name", name);
                finalPage.putExtra("belonging", belong);
                finalPage.putExtra("users age", 
 ageFind.getText().toString());
                startActivity(finalPage);

            }
        }
    });
 }

}

activity_ageInput.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/agefact2">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        app:srcCompat="@drawable/type13"
        tools:ignore="ContentDescription" />



    <LinearLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="6sp"
        android:background="@drawable/edit_background"
        android:padding="2dp"
        android:weightSum="1">

        <EditText
            android:id="@+id/inputAge"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/edit_text"
            android:ems="10"
            android:hint="@string/age"
            android:inputType="text"
            android:padding="10dp"
            android:textSize="18sp"
            android:layout_margin="3dp"
            android:layout_weight="1.02"
            android:textColor="@color/colorAccent"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/ageButton"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            app:backgroundTint="#32CD32"
            app:elevation="2dp"
            app:fabSize="normal"
            app:rippleColor="#7FFF00"
            app:srcCompat="@drawable/ic_keyboard_arrow_right_white_24dp" />
    </LinearLayout>

  </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

edit_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="1dp">

<stroke
    android:color="#696969"
    android:width="2dp"/>
<solid android:color="#FFFFFF"></solid>

<corners
    android:radius="25dp" />

</shape>

edit_text.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">

</shape>

崩溃报告

07-02 13:29:40.966 14586-14586 / randomexcessor.foutuneteller E / AndroidRuntime:FATAL EXCEPTION:main进程:randomexcessor.foutuneteller,PID:14586 java.lang.OutOfMemoryError:无法分配带有15898356个空闲字节的19743564字节分配在android.graphics.BitmapFactory.decodeResourceStream的android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)上的android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)中的dal at dalvik.system.VMRuntime.newNonMovableArray(Native Method)的15MB (BitmapFactory.java:444)位于android.content.res.Resources的android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)上的android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)。 loadDrawable(Resources.java:2384)位于android.content.res.Resources.getDrawable(Resources.java:787)的android.content.Context.getDrawable(Context.java:403),位于android.support.v4.content.ContextCompatApi21 android.support中的.getDrawable(ContextCompatApi21.java:30)位于android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)的.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)位于android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java) :190)位于android.support.v7上的android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:54)。 widget.AppCompatImageView。(AppCompatImageView.java:66)在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:56)在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)在android位于android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)的.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029),位于android.support.v4.view.LayoutInflaterCompatHC $ FactoryWrapperHC.onCreateView (LayoutInflaterCompatHC.j ava:47)在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)的android.view.LayoutInflater.rInflate(LayoutInflater.java:806)的android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) android.view.LayoutInflater.inflate(LayoutInflater.java:504)android.view.LayoutInflater.inflate(LayoutInflater.java:414)android.view.LayoutInflater.inflate(LayoutInflater.java:365)android.support.v7位于Android的randomexcessor.foutuneteller.ageInput.onCreate(ageInput.java:22)的android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)上的.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)。 android.app.ActivityThread.handleLaunchActivity上android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)上的app.Activity.performCreate(Activity.java:6010) (ActivityThread.java:2413)在android.app.ActivityThread.access $ 800(ActivityT hread.java:155)在Android.os.Looper.loop(Looper.java)android.app.Handler.dispatchMessage(Handler.java:102)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1317) :135)在android.app.ActivityThread.main(ActivityThread.java:5343)的java.lang.reflect.Method.invoke(Native Method)java.lang.reflect.Method.invoke(Method.java:372)at at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

java android xml android-intent
1个回答
0
投票

崩溃报告的第一级显示OOM,即OutOfMemory。您放在activity_ageInput.xml中的任何位图或者您在Toast布局中使用的位图都太大(或所有位图)。您应该考虑减小位图的大小。阅读关于处理位图的这个帖子:

https://developer.android.com/topic/performance/graphics/index.html

© www.soinside.com 2019 - 2024. All rights reserved.