致命异常:主要和不支持作为命令运行

时间:2016-10-28 19:56:02

标签: java android xml galaxy

我试图制作一个应用程序,您可以在其中输入电话号码和消息,然后按"发送"按钮,它会发送它。但是当我尝试在我的手机(三星Galaxy S7)上运行它时,我收到错误"致命异常:主要"。我试过查找解决方案,但没有一个能为我的项目工作:

MainActivity.java

package com.example.skiddswarmik.myfirstproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText getPhoneNumber = (EditText)findViewById(R.id.number);
String numberFinal = getPhoneNumber.getText().toString();

EditText getMessage = (EditText)findViewById(R.id.message);
String finalMessage = getMessage.getText().toString();

// globally
EditText errorMessage = (EditText)findViewById(R.id.errorMessage);


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

//When Button is Pressed
public void buttonOnClick(View v){


    //Setting alphabet as variable
    boolean match = numberFinal.matches("[a-zA-Z]+");

    //Check if phone number contains letters
    if(match==true){
        errorMessage.setText("*Illgal characters used in Phone Number");
    }

    //if phone number is all digits
    else{
        //if textfields are empty
        if(numberFinal.length()==0 | errorMessage.length()==0){
            errorMessage.setText("*All Textfields are required");
        }
        //TextField is full
        else {
            //if the phone number has less than four digits
            if (getPhoneNumber.length() < 4) {
                errorMessage.setText("*Phone number too short(4min)");
                //if phone number has more than four digits
            } else {
                //if message is less than two
                if (finalMessage.length() <= 1) {
                    errorMessage.setText("*Message is too short(2min)");
                    //if message is more than five thousand
                } else if (finalMessage.length() >= 5000) {
                    errorMessage.setText("*Message is too long (5000max)");
                }

                //RUN
                else {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(numberFinal, null, finalMessage, null, null);
                }
            }
        }
    }



}}

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:id="@+id/activity_main"
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="com.example.skiddswarmik.myfirstproject.MainActivity">


<EditText
    android:id="@+id/number"
    android:layout_width="1000px"
    android:layout_height="100px"
    android:inputType="textMultiLine"
    android:ems="10"
    android:background="#f1f1f1"
    android:hint="@string/phone_number"
    android:paddingLeft="20px"
    android:paddingRight="20px"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    />


<EditText
    android:id="@+id/message"
    android:layout_width="1000px"
    android:layout_height="400px"
    android:inputType="textMultiLine"
    android:ems="10"
    android:layout_below="@+id/number"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="64dp"
    android:hint="@string/message"
    android:paddingTop="20px"
    android:paddingLeft="20px"
    android:paddingRight="20px"
    android:gravity="top"
    android:background="#f1f1f1"
    tools:ignore="RtlSymmetry" />


<Button
    android:id="@+id/sendButton"
    android:text="@string/send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/message"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp"
    android:onClick="buttonOnClick (MainActivity)"
    android:visibility="visible" />


<EditText
    android:id="@+id/errorMessage"
    android:text="@string/no_error"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/sendButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:textColor="#ff1919"/>

的AndroidManifest.xml

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

<uses-permission android:name="android.permission.SEND_SMS" />

<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>
</application>

2 个答案:

答案 0 :(得分:0)

将变量的初始化放在onCreate函数中:

public class MainActivity extends AppCompatActivity {


EditText getPhoneNumber;
String numberFinal;

EditText getMessage;
String finalMessage;

// globally
EditText errorMessage;

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

    getPhoneNumber = (EditText)findViewById(R.id.number);
    numberFinal = getPhoneNumber.getText().toString();

    getMessage = (EditText)findViewById(R.id.message);
    finalMessage = getMessage.getText().toString();

    errorMessage = (EditText)findViewById(R.id.errorMessage);


}

}

答案 1 :(得分:0)

试试这个:

<div class="main-image-container">
  <img src="http://i.imgur.com/VwQjLZI.jpg" class="main-image" onload="imageExists(this)">
  <img src="" data-src="http://i.imgur.com/VwQjLZI.jpg" class="secondary-image" onload="this.className+=' loaded';">
  <img src="" data-src="http://i.imgur.com/VwQjLZI.jpg" class="secondary-image" onload="this.className+=' loaded';">
  <img src="" data-src="http://google.com/does_not_exist.jpg" class="secondary-image" onload="this.className+=' loaded';">
  <img src="" data-src="http://i.imgur.com/VwQjLZI.jpg" class="secondary-image" onload="this.className+=' loaded';">
</div>