应用程序在通过Intent启动新活动时崩溃

时间:2017-04-12 13:08:24

标签: java android xml android-intent

这是我在Android Studio中为学校项目构建的第一个应用程序,我试图通过单击按钮将数据发送到MainActivity中的另一个活动。单击按钮时应用程序崩溃。 screenshot of the error我不知道失败的地方和解决方案是什么。

这是MainActivity.java

public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private EditText maxticketSrc;

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

    public void sendMessage(View view) {
        Intent intent = new Intent(this, ScanActivity.class);
        EditText eventSrc = (EditText) findViewById(R.id.eventEditTxt);
        EditText sectorSrc = (EditText) findViewById(R.id.sectorEditTxt);
        EditText minticketSrc = (EditText) findViewById(R.id.minticketEditTxt);
        maxticketSrc = (EditText) findViewById(R.id.maxticketEditTxt);
        String eventTxt = eventSrc.getText().toString();
        String sectorTxt = sectorSrc.getText().toString();
        String minticketTxt = minticketSrc.getText().toString();
        String maxticketTxt = maxticketSrc.getText().toString();
        String message = eventTxt + "-" + sectorTxt + "-" + minticketTxt + "-" + maxticketTxt;
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

这是activity_main.xml

的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.abinodh.ddm.ticketvalidator.ScanActivity">

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Event Setup Wizard"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintHorizontal_bias="0.452"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="28dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="171dp"
    android:layout_height="35dp"
    android:text="Event ID:"
    android:textSize="20sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.117"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.191" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="172dp"
    android:layout_height="34dp"
    android:text="Max Ticket No:"
    android:textAlignment="textStart"
    android:textSize="20sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.117"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.572" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="171dp"
    android:layout_height="34dp"
    android:text="Min Ticket No:"
    android:textAlignment="textStart"
    android:textSize="20sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.117"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.44" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="171dp"
    android:layout_height="33dp"
    android:text="Sector ID:"
    android:textAlignment="textStart"
    android:textSize="20sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.117"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.313" />

<EditText
    android:id="@+id/maxticketEditTxt"
    android:layout_width="160dp"
    android:layout_height="34dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.875"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintVertical_bias="0.572" />

<EditText
    android:id="@+id/minticketEditTxt"
    android:layout_width="161dp"
    android:layout_height="34dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.878"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.44" />

<EditText
    android:id="@+id/sectorEditTxt"
    android:layout_width="161dp"
    android:layout_height="33dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.878"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.313" />

<EditText
    android:id="@+id/eventEditTxt"
    android:layout_width="161dp"
    android:layout_height="35dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.878"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.191" />

<Button
    android:id="@+id/setupWizSubmitBtn"
    android:layout_width="188dp"
    android:layout_height="66dp"
    android:text="Submit"
    android:onClick="sendMessage"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

这是ScanActivity.java

public class ScanActivity extends AppCompatActivity implements View.OnClickListener {

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String[] messageArray = message.split("-");
int eventData = Integer.parseInt(messageArray[0]);
int sectorData = Integer.parseInt(messageArray[1]);
int minticketData = Integer.parseInt(messageArray[2]);
int maxticketData = Integer.parseInt(messageArray[3]);

private TextView resultTxt, contentTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    Button scanBtn = (Button) findViewById(R.id.scan_button);
    resultTxt = (TextView)findViewById(R.id.scan_format);
    contentTxt = (TextView)findViewById(R.id.scan_content);
    scanBtn.setOnClickListener(this);
}
public void onClick(View v){
    if(v.getId()==R.id.scan_button){
        IntentIntegrator scanIntegrator = new IntentIntegrator(this);
        scanIntegrator.initiateScan();
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        Boolean validity = true;
        String scanContent = scanningResult.getContents();
        String[] resultArr = scanContent.split("-");
        int event = Integer.parseInt(resultArr[0]);
        int sector = Integer.parseInt(resultArr[1]);
        int ticket = Integer.parseInt(resultArr[2]);
        String validityStr;
        if(event != eventData)
        {
            validity = false;
        }
        if(sector != sectorData)
        {
            validity = false;
        }
        if(ticket < minticketData || ticket > maxticketData)
        {
            validity = false;
        }
        if (validity)
            validityStr = "Valid";
        else
            validityStr = "Not Valid";
        resultTxt.setText("Event: " + event + "\nSector: " + sector + "\nTicket Number: " + ticket + "\nValidity: " + validityStr);
        contentTxt.setText("Scan Result: " + scanContent);
    }
    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }
}

}

这是activity_scan.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.abinodh.ddm.ticketvalidator.MainActivity">

<Button
    android:id="@+id/scan_button"
    android:layout_width="368dp"
    android:layout_height="134dp"
    android:layout_centerHorizontal="true"
    android:text="SCAN"
    android:textSize="45sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.029" />

<TextView
    android:id="@+id/scan_format"
    android:layout_width="333dp"
    android:layout_height="221dp"
    android:layout_below="@id/scan_button"
    android:layout_centerHorizontal="true"
    android:text="Please Scan"
    android:textAlignment="center"
    android:textIsSelectable="true"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/scan_content"
    android:layout_width="409dp"
    android:layout_height="43dp"
    android:text="Content"
    android:textAlignment="center"
    android:textSize="20sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.82"
    app:layout_constraintHorizontal_bias="0.48" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="179dp"
    android:layout_height="70dp"
    app:srcCompat="@mipmap/ic_launcher"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.988" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline"
    app:layout_constraintGuide_begin="20dp"
    android:orientation="horizontal"
    tools:layout_editor_absoluteY="20dp"
    tools:layout_editor_absoluteX="0dp" />

</android.support.constraint.ConstraintLayout>

4 个答案:

答案 0 :(得分:1)

您是否尝试过在清单中添加活动? 请添加并尝试运行该应用。

答案 1 :(得分:0)

intent.putExtra(EXTRA_MESSAGE, message);替换为intent.putExtra("EXTRA_MESSAGE", message);

并以这种方式检索:String message = getIntent().getExtras().getString("EXTRA_MESSAGE");

请在您的问题中提供Logcat错误,以便我们为您提供帮助。

答案 2 :(得分:0)

问题在于您如何访问意图及其在第二项活动中的额外内容。

来自:

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String[] messageArray = message.split("-");
int eventData = Integer.parseInt(messageArray[0]);
int sectorData = Integer.parseInt(messageArray[1]);
int minticketData = Integer.parseInt(messageArray[2]);
int maxticketData = Integer.parseInt(messageArray[3]);

protected void onCreate(Bundle savedInstanceState){
  ...
}

为:

Intent intent;
String message ;
String[] messageArray;
int eventData;
int sectorData;
int minticketData;
int maxticketData;

protected void onCreate(Bundle savedInstanceState) {
  ...

  intent = getIntent();
  message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  messageArray = message.split("-");
  eventData = Integer.parseInt(messageArray[0]);
  sectorData = Integer.parseInt(messageArray[1]);
  minticketData = Integer.parseInt(messageArray[2]);
  maxticketData = Integer.parseInt(messageArray[3]);
}

当框架加载或实例化类时,Intent不可用。

将所有类字段初始化移至onCreate方法。 getIntent()方法实际上会返回对象及其所有附加内容。

答案 3 :(得分:0)

@覆盖

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

    EditText eventSrc = (EditText) findViewById(R.id.eventEditTxt);
    EditText sectorSrc = (EditText) findViewById(R.id.sectorEditTxt);
    EditText minticketSrc = (EditText) findViewById(R.id.minticketEditTxt);
    maxticketSrc = (EditText) findViewById(R.id.maxticketEditTxt);
    Button setupWizSubmitBtn =(Button) findViewByID(R.id.setupWizSubmitBtn); 

setupWizSubmitBtn.setOnClickListner(new OnClickListner()    {

 @Override

   public void onClick(View v) {

  Intent intent = new Intent(this, ScanActivity.class);
    String eventTxt = eventSrc.getText().toString();
    String sectorTxt = sectorSrc.getText().toString();
    String minticketTxt = minticketSrc.getText().toString();
    String maxticketTxt = maxticketSrc.getText().toString();
    String message = eventTxt + "-" + sectorTxt + "-" + minticketTxt + "-" +                   maxticketTxt;
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
 }

});

}