2017-09-26 84 views
0

我正在进行一项简单的评分应用程序项目,其中包含3项活动,第一项是在数据库中存储一些数据,并且在第一项活动和第三项活动中复制该数据。在活动之间传递数据Forces Close

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

我试图显示这个结果在一个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

粘贴错误日志 – YoLo

+0

在第三个活动R.id.editText15这一个你的类型转换到TextView的? – Anonymous

回答

1

从删除捆绑您的代码和使用以下代码

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

在你第三个活动

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