简体   繁体   English

修复 android studio 制作的应用程序不断停止

[英]Fix app keeps stopping made with android studio

I'm new in the android development field when I try to transfer data between activities can't get my message and app got stopping show me app keep stopping我是 android 开发领域的新手,当我尝试在活动之间传输数据时无法收到我的消息并且应用程序停止显示应用程序不断停止

MainActivity.java MainActivity.java

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

    Intent outIntent;
    EditText edtPhone;
    EditText edtMessage;
    Button btnNext;
    String tempText="";

   public static final String PHONE = "PHONE";
   public static final  String MESSAGE = "MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View V) {
                outIntent = new Intent(MainActivity.this, ActivityTwo.class);
                edtPhone = (EditText) findViewById(R.id.edtPhone);
                edtMessage = (EditText) findViewById(R.id.edtMessage);
                tempText = edtPhone.getText().toString();
                outIntent.putExtra(PHONE,tempText);
                tempText = edtMessage.getText().toString();
                outIntent.putExtra(MESSAGE,tempText);

                startActivity(outIntent);
            }

    });
}
    public void closeMethod(View view) {
        finish();
    }
}

ActivityTwo.java活动二.java

package com.example.fir;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

public class ActivityTwo extends AppCompatActivity {

        Intent incomingIntent;
        TextView txtPhone;
        TextView txtMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        incomingIntent = getIntent();
        txtPhone = (TextView) findViewById(R.id.txtPhone);
        txtMessage = (TextView) findViewById(R.id.txtMessage);
        txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
        txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));

    }
}

Activitymain.xml活动main.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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Phone" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="60dp"
        android:ems="10"
        android:hint="Phone"
        android:inputType="phone"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="124dp"
        android:text="Message" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="3dp"
        android:ems="10"
        android:hint="Message"
        android:inputType=""
        android:minHeight="48dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="224dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="closeMethod"
            android:text="CLose" />
    </LinearLayout>
</RelativeLayout>
Activitytwo.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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:text="Phone" />

    <EditText
        android:id="@+id/editTextPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="60dp"
        android:ems="10"
        android:hint="Phone"
        android:inputType="phone"
        android:minHeight="48dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="124dp"
        android:text="Message" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="3dp"
        android:ems="10"
        android:hint="Message"
        android:inputType=""
        android:minHeight="48dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="224dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="closeMethod"
            android:text="CLose" />
    </LinearLayout>
</RelativeLayout>

my problem is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button the app crashes and a pop up shows "myapp keeps stopping".我的问题是,当我没有将任何值放入 firstEditText 或 secondEditText 或它们两者中并单击任何按钮时,应用程序崩溃并弹出显示“myapp 不断停止”。 i cannot get the text..我无法得到文本..

you have defined your Edit texts in the first activity with id's named您已经在第一个活动中定义了您的编辑文本,id 名为

edtPhone
edtMessage

but there was no edtPhone or edtMessage in your xml. this issue also repeats in your second activity you have defined txtPhone and txtMessage but again there are no such ids in your second activity XML. keep in mind that these might not be the whole problem as you didn't post any log for the errors.但是在您的 xml 中没有edtPhoneedtMessage 。这个问题在您定义txtPhonetxtMessage的第二个活动中也会重复,但在您的第二个活动 XML 中同样没有这样的 ID。请记住,这些可能不是全部问题,因为您没有发布任何错误日志。 but if there were anything else, ask and we'll help you但如果还有其他问题,请提出来,我们会帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM