简体   繁体   English

单击Android Studio中的按钮后,Android应用程序崩溃

[英]Android app crashes on click of button in Android Studio

I'm fourteen and new to java and android studio and i'm making a simple application that collects user recharge card pin as input and then dials it to the phone as a USSD code but everytime any of the buttons are clicked on the MainActivity.xml, the app crashes. 我今年14岁,是java和android studio的新手,我正在制作一个简单的应用程序,该程序收集用户充值卡的针脚作为输入,然后将其作为USSD代码拨入电话,但是每次在MainActivity上单击任何按钮时。 xml,应用崩溃。 These are the codes 这些是代码

//MainActivity.java


 package devchuks.com.rechargeyourcard;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

        public void openEtisalat (View v) {
            Intent intent = new Intent(this, Etisalat.class);
            startActivity(intent);
        }

        public void openMTN (View v) {
            Intent intent2 = new Intent(this, MTNactivity.class);
            startActivity(intent2);
        }
        public void openAirtel (View v) {
            Intent intent3 = new Intent(this, AirtelActivity.class);
            startActivity(intent3);
        }
        public void openGlo (View v) {
            Intent intent4 = new Intent(this, GloActivity.class);
            startActivity(intent4);
        }
    }

}

This is the xml of mainactivity 这是mainactivity的xml

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="devchuks.com.rechargeyourcard.MainActivity">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/linearLayout">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Etisalat"
            android:id="@+id/button"
            android:onClick="openEtisalat"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/MTN"
            android:id="@+id/button2"
            android:onClick="openMTN"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/Airtel"
            android:id="@+id/button3"
            android:onClick="openAirtel"/>

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Glo"
            android:id="@+id/button4"
            android:onClick="openGlo"/>

        <TextView
            android:layout_width="218dp"
            android:layout_height="wrap_content"
            android:text="@string/copyright"
            android:id="@+id/textView"
            android:layout_marginRight="255dp"
            android:layout_marginEnd="255dp" />
    </LinearLayout>

</RelativeLayout>

this is an example of one of the activities to be opened 这是将要开展的活动之一的示例

//Etisalat.java
package devchuks.com.rechargeyourcard;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

import devchuks.com.rechargeyourcard.R;

public class Etisalat extends Activity {

    private Button callBtn;
    private Button dialBtn;
    private EditText number;

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

        number = (EditText) findViewById(R.id.phoneNumber);
        callBtn = (Button) findViewById(R.id.call);

        // add PhoneStateListener for monitoring
        MyPhoneListener phoneListener = new MyPhoneListener();
        TelephonyManager telephonyManager =
                (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        // receive notifications of telephony state changes
        telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

        callBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    // set the data
                    String uri = "tel:" + "*555*" + number.getText().toString() + "#";
                    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));

                    startActivity(callIntent);
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Your call has failed...",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });

        dialBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    String ussd = number.getText().toString();
                    String uri = "tel:" + "*555*" + ussd + Uri.encode("#");
                    Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(uri));

                    startActivity(dialIntent);
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Your call has failed...",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
    }

    private class MyPhoneListener extends PhoneStateListener {

        private boolean onCall = false;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    // phone ringing...
                    Toast.makeText(Etisalat.this, incomingNumber + " calls you",
                            Toast.LENGTH_LONG).show();
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    // one call exists that is dialing, active, or on hold
                    Toast.makeText(Etisalat.this, "on call...",
                            Toast.LENGTH_LONG).show();
                    //because user answers the incoming call
                    onCall = true;
                    break;

                case TelephonyManager.CALL_STATE_IDLE:
                    // in initialization of the class and at the end of phone call

                    // detect flag from CALL_STATE_OFFHOOK
                    if (onCall == true) {
                        Toast.makeText(Etisalat.this, "restart app after call",
                                Toast.LENGTH_LONG).show();

                        // restart our application
                        Intent restart = getBaseContext().getPackageManager().
                                getLaunchIntentForPackage(getBaseContext().getPackageName());
                        restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(restart);

                        onCall = false;
                    }
                    break;
                default:
                    break;
            }

        }
    }

}

and it's xml 这是xml

 //EtisalatActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="devchuks.com.rechargeyourcard.Etisalat">
    <EditText
        android:id="@+id/phoneNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="@string/pin"
        android:ems="10"
        android:inputType="phone" />

    <Button
        android:id="@+id/call"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/phoneNumber"
        android:layout_marginTop="20dp"
        android:text="@string/Recharge" />

</RelativeLayout>

Please help fix this 请帮助解决此问题

Here's my Manifest.xml 这是我的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="devchuks.com.rechargeyourcard">
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".Etisalat" />
        <activity android:name=".MTNactivity" />
        <activity android:name=".AirtelActivity" />
        <activity android:name=".GloActivity" />
    </application>

</manifest>

First - The cause of crashes, you will be able to see it in the tab of Log. 首先-崩溃原因,您将可以在“日志”标签中看到它。 Second - (You did not share the Manifest) The likely cause of crashes is that you did not declare the activitys in the manifest. 其次-(您没有共享清单)崩溃的可能原因是您没有在清单中声明活动。

首先,您必须将Etisalat活动的布局文件更改为“ R.layout.activity_main”,以将其更改为“ R.layout.EtisalatActivity”,然后看看会发生什么,并且仍然崩溃,请共享您的logcat。

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

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