在活动之间传递数据强制关闭

时间:2017-09-26 11:49:57

标签: java android android-intent textview

我正在开发一个包含3个活动的简单Grading应用程序项目,第一个是在数据库中存储一些数据,我在第一个活动和第三个活动中复制这些数据。

第二项活动是进行平均计算并在同一页面上显示结果,但我希望该结果也显示在第三页上。我尝试使用意图,但当我点击按钮转到第三页时,它强制关闭。我究竟做错了什么。

我试图在textview中显示此结果

这是第二个活动代码:

public class AverageActivity extends AppCompatActivity {
EditText editmanner, editinstances, editshortstance, editstrikes, editboxingskills, editknocks, editkicks, editResults;
Button btnResults, btnnewresults;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.average_page);
        editmanner = (EditText) findViewById(R.id.editText8);
        editinstances = (EditText) findViewById(R.id.editText9);
        editshortstance = (EditText) findViewById(R.id.editText10);
        editstrikes = (EditText) findViewById(R.id.editText11);
        editboxingskills = (EditText) findViewById(R.id.editText12);
        editknocks = (EditText) findViewById(R.id.editText13);
        editkicks = (EditText) findViewById(R.id.editText14);
        editResults = (EditText) findViewById(R.id.editText15);
        btnResults = (Button) findViewById(R.id.button10);
        btnnewresults = (Button) findViewById(R.id.botonresultnuevo);

        btnResults.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                int first;
                if (editmanner.getText().toString().equals("")) {
                    first = 0;
                } else {
                    first = Integer.valueOf(editmanner.getText().toString());
                }
                int second;
                if (editinstances.getText().toString().equals("")) {
                    second = 0;
                } else {
                    second = Integer.valueOf(editinstances.getText().toString());
                }
                int third;
                if (editshortstance.getText().toString().equals("")) {
                    third = 0;
                } else {
                    third = Integer.valueOf(editshortstance.getText().toString());
                }
                int fourth;
                if (editstrikes.getText().toString().equals("")) {
                    fourth = 0;
                } else {
                    fourth = Integer.valueOf(editstrikes.getText().toString());
                }
                int fifth;
                if (editboxingskills.getText().toString().equals("")) {
                    fifth = 0;
                } else {
                    fifth = Integer.valueOf(editboxingskills.getText().toString());
                }
                int sixth;
                if (editknocks.getText().toString().equals("")) {
                    sixth = 0;
                } else {
                    sixth = Integer.valueOf(editknocks.getText().toString());
                }
                int seventh;
                if (editkicks.getText().toString().equals("")) {
                    seventh = 0;
                } else {
                    seventh = Integer.valueOf(editkicks.getText().toString());
                }
                int results;


                first = Integer.parseInt(editmanner.getText().toString());
                second = Integer.parseInt(editinstances.getText().toString());
                third = Integer.parseInt(editshortstance.getText().toString());
                fourth = Integer.parseInt(editstrikes.getText().toString());
                fifth = Integer.parseInt(editboxingskills.getText().toString());
                sixth = Integer.parseInt(editknocks.getText().toString());
                seventh = Integer.parseInt(editkicks.getText().toString());
                results = (first + second + third + fourth + fifth + sixth + seventh) / 7;
                editResults.setText(String.valueOf(results));


            }

        });


    }

    public void knowtheresults(View view) {
        switch (view.getId()) {
            case R.id.botonresultnuevo:
                Intent miintent = new Intent(AverageActivity.this, ResultActivity.class);
                Bundle miBundle = new Bundle();
                miBundle.putString("nombre", editResults.getText().toString());
                miintent.putExtras(miBundle);
                startActivity(miintent);
                break;


        }
        String button_text;
        button_text = ((Button) view).getText().toString();
        if (button_text.equals("Summary")) {
            Intent intent = new Intent(this, ResultActivity.class);
            startActivity(intent);
        }
    }
}

这是第三个活动代码:

public class ResultActivity extends Activity {

TextView texto;
DatabaseHelper mDatabaseHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_page);
    texto = (TextView) findViewById(R.id.editText15);
    Bundle mibundle=this.getIntent().getExtras();


    if(mibundle!=null){

    String dato = mibundle.getString("nombre");
        texto.setText(dato);
    }


    mDatabaseHelper = new DatabaseHelper(this);
    displayDatabaseInfo();
}
private void displayDatabaseInfo() {
    // To access our database, we instantiate our subclass of SQLiteOpenHelper
    // and pass the context, which is the current activity.
    DatabaseHelper mDbHelper = new DatabaseHelper(this);

    // Create and/or open a database to read from it
    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    // Perform this raw SQL query "SELECT * FROM pets"
    // to get a Cursor that contains all rows from the pets table.
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

    TextView displayView = findViewById(R.id.textViewR1);
    try {

        displayView.setText("The Student...\n\n");
        displayView.append(COL1 + "--" +
                COL2 + "--" +
                COL4 +
                 "\n");
        // Figure out the index
        int idColumnIndex = cursor.getColumnIndex(COL1);
        int nameColumnIndex = cursor.getColumnIndex(COL2);
        int rankColumnIndex = cursor.getColumnIndex(COL4);


        while (cursor.moveToNext()) {


            int currentID = cursor.getInt(idColumnIndex);
            String currentName = cursor.getString(nameColumnIndex);

            String currenRank = cursor.getString(rankColumnIndex);

            displayView.append(currentID + "--" +
                    currentName + "--" +
                    currenRank + "\n");
        }
    } finally {
        // Always close the cursor when you're done reading from it. This releases all its
        // resources and makes it invalid.
        cursor.close();
    }
}


public void knowtheresults(View view) {
    String button_text;
    button_text = ((Button) view).getText().toString();
    if (button_text.equals("Start Page")) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    } else if (button_text.equals("Back...")) {
        Intent intent = new Intent(this, AverageActivity.class);
        startActivity(intent);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

从您的代码中删除Bundle并使用以下代码

Intent miintent = new Intent(AverageActivity.this, ResultActivity.class);
miintent.putString("nombre", editResults.getText().toString());
startActivity(miintent);

在你的第三个活动中

String number = getIntent().getStringExtra("nombre");