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

时间:2022-05-18 23:10:03

This is my first app which I am building for a school project in Android Studio and I am trying to send data over to another activity from MainActivity through a button click. The app crashes when I click the button. screenshot of the error I don't have any idea where it is failing and what the fix is.

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

This is the MainActivity.java

这是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);
    }

}

This is the XML Layout source for activity_main.xml

这是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" />

And this is ScanActivity.java

这是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();
    }
}

}

}

This is code inside activity_scan.xml

这是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 个解决方案

#1


0  

The problem is how you are accessing the intent and it's extras on the second activity.

问题是你如何访问意图,以及第二个活动的附加内容。

from :

来自:

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){
  ...
}

to:

至:

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 is not available when the class is loaded or instantiated by the framework.

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

Move all your class fields initialization to the onCreate method. The getIntent() method will actually return the object and all it's extras.

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

#2


1  

Have you tried by adding Activity in manifest? Please add and try to run the app.

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

#3


0  

replace intent.putExtra(EXTRA_MESSAGE, message); with intent.putExtra("EXTRA_MESSAGE", message);

替换intent.putExtra(EXTRA_MESSAGE,message); with intent.putExtra(“EXTRA_MESSAGE”,message);

and retrieve this way: String message = getIntent().getExtras().getString("EXTRA_MESSAGE");

并检索这种方式:String message = getIntent()。getExtras()。getString(“EXTRA_MESSAGE”);

Please, provide Logcat error in your question, so we can help you.

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

#4


0  

@Override

@覆盖

protected void onCreate(Bundle savedInstanceState) {

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() {

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);
 }

});

});

}

}

#1


0  

The problem is how you are accessing the intent and it's extras on the second activity.

问题是你如何访问意图,以及第二个活动的附加内容。

from :

来自:

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){
  ...
}

to:

至:

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 is not available when the class is loaded or instantiated by the framework.

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

Move all your class fields initialization to the onCreate method. The getIntent() method will actually return the object and all it's extras.

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

#2


1  

Have you tried by adding Activity in manifest? Please add and try to run the app.

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

#3


0  

replace intent.putExtra(EXTRA_MESSAGE, message); with intent.putExtra("EXTRA_MESSAGE", message);

替换intent.putExtra(EXTRA_MESSAGE,message); with intent.putExtra(“EXTRA_MESSAGE”,message);

and retrieve this way: String message = getIntent().getExtras().getString("EXTRA_MESSAGE");

并检索这种方式:String message = getIntent()。getExtras()。getString(“EXTRA_MESSAGE”);

Please, provide Logcat error in your question, so we can help you.

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

#4


0  

@Override

@覆盖

protected void onCreate(Bundle savedInstanceState) {

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() {

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);
 }

});

});

}

}