Android:在两个活动之间切换错误 - 不幸的是“App Name”已停止

时间:2017-04-21 14:48:58

标签: java android android-intent

我正在学习Android应用程序开发,然后出现了这个问题,我不确定人们之前是否有这个问题,但它说我的应用程序已停止在模拟器中。这是一种基本的货币转换器。

主要活动:

public class MainActivity extends Activity implements 
    View.OnClickListener {
    private final int GET_SECACT_RES = 6;
    public TextView result;
    public EditText amount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myB = (Button)findViewById(R.id.btn1);
        myB.setOnClickListener(this);
        amount =(EditText)findViewById(R.id.amount);
        result=(TextView)findViewById(R.id.textView);

    }

    public void onClick(View v){
        Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
        myIntent.putExtra("amount", amount.getText().toString());
        startActivityForResult(myIntent, GET_SECACT_RES);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == GET_SECACT_RES){
            if(resultCode == Activity.RESULT_OK){
                String txt = data.getStringExtra("result");
                result.setText(txt);
            }
        }
    }
}

第二项活动:

public class SecondActivity extends Activity implements View.OnClickListener {
    public Double result;

    RadioButton usDollar = (RadioButton) findViewById(R.id.usDollar);
    RadioButton japaneseYen = (RadioButton) findViewById(R.id.japaneseYen);
    String amountAdded = getIntent().getStringExtra("amount");
    Double doubleAmount = Double.parseDouble(amountAdded);


    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Button myB = (Button) findViewById(R.id.btn2);
        myB.setOnClickListener(this);
    }

    public void calculate() {
        if(usDollar.isSelected()){
            result = 1.09 * doubleAmount;
        }
        if (japaneseYen.isSelected()) {
           result = 0.7 * doubleAmount;
        }
    }
    public void onClick(View v){
        calculate();
        Intent res = new Intent(SecondActivity.this, MainActivity.class);
        res.putExtra("result", result.toString());
        setResult(RESULT_OK,res);
        finish();
    }
}

清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    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>
    <activity android:name=".SecondActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

logcat的:

04-21 18:01:01.085 7559-7559/? I/art: Not late-enabling -Xcheck:jni (already on)
04-21 18:01:01.086 7559-7559/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-21 18:01:01.413 7559-7559/com.example.calculatorlab5 W/System: ClassLoader referenced unknown path: /data/app/com.example.calculatorlab5-2/lib/x86
04-21 18:01:01.516 7559-7559/com.example.calculatorlab5 I/InstantRun: Starting Instant Run Server for com.example.calculatorlab5


[ 04-21 18:01:01.566  7559: 7587 D/         ]

HostConnection::get() New Host Connection established 0x9abc6300, tid 7587
04-21 18:01:01.573 7559-7587/com.example.calculatorlab5 I/OpenGLRenderer: Initialized EGL, version 1.4
04-21 18:01:01.573 7559-7587/com.example.calculatorlab5 D/OpenGLRenderer: Swap behavior 1
04-21 18:01:01.574 7559-7587/com.example.calculatorlab5 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-21 18:01:01.574 7559-7587/com.example.calculatorlab5 D/OpenGLRenderer: Swap behavior 0

请帮忙。非常感谢你。

1 个答案:

答案 0 :(得分:0)

您在此处有错误:

  Double doubleAmount = Double.parseDouble("amountAdded"); 

从amountAdded中删除引号。

 Double doubleAmount = Double.parseDouble(amountAdded);