activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.uncc.sbagursu.currencyconvertor.MainActivity">
<EditText
android:hint="Input Amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/inputAmount" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/inputAmount"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="@+id/radioGroup2">
<RadioButton
android:id="@+id/aud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/aud"
android:onClick="onRadioButtonClicked1"/>
<RadioButton
android:id="@+id/cad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cad"
android:onClick="onRadioButtonClicked1"/>
<RadioButton
android:id="@+id/inr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inr"
android:onClick="onRadioButtonClicked1"/>
</RadioGroup>
<TextView
android:text="@string/convertTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:id="@+id/textView2"
android:layout_below="@+id/radioGroup2"
android:layout_alignRight="@+id/radioGroup2"
android:layout_alignEnd="@+id/radioGroup2" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/inputAmount"
android:layout_centerHorizontal="true"
android:layout_marginTop="170dp"
android:id="@+id/radioGroup3">
<RadioButton
android:id="@+id/usd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/usd"
android:onClick="onRadioButtonClicked2"/>
<RadioButton
android:id="@+id/gbp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gbp"
android:onClick="onRadioButtonClicked2"/>
</RadioGroup>
<Button
android:text="@string/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup3"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="@+id/convertButton"
android:onClick="onClickConvert"/>
<TextView
android:text="@string/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/convertButton"
android:layout_alignLeft="@+id/radioGroup3"
android:layout_alignStart="@+id/radioGroup3"
android:layout_marginTop="28dp"
android:id="@+id/textView3" />
<Button
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_alignLeft="@+id/convertButton"
android:layout_alignStart="@+id/convertButton"
android:layout_marginTop="23dp"
android:id="@+id/clearButton" />
</RelativeLayout>
MainActivity.java:
package com.example.shara.currencyconvertor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView txt = (TextView) findViewById(R.id.textView3);
private RadioGroup grp1 = (RadioGroup) findViewById(R.id.radioGroup2);
private RadioGroup grp2 = (RadioGroup) findViewById(R.id.radioGroup3);
private EditText inputAmt = (EditText) findViewById(R.id.inputAmount);
private final String inputText = inputAmt.getText().toString();
private double inputAmount = Integer.parseInt(inputText);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.convertButton).setOnClickListener(this);
findViewById(R.id.clearButton).setOnClickListener(this);
grp1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton) findViewById(checkedId);
}
});
grp2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn2 = (RadioButton) findViewById(checkedId);
}
});
}
@Override
public void onClick(View v) {
int radioId1 = grp1.getCheckedRadioButtonId();
int radioId2 = grp2.getCheckedRadioButtonId();
double result = 0;
if(v.getId()==R.id.convertButton){
if(radioId1 == R.id.aud){
if(radioId2 == R.id.usd){
result = inputAmount*1.34;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/1.34);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
if(radioId1 == R.id.cad){
if(radioId2 == R.id.usd){
result = inputAmount*1.32;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if (radioId2 == R.id.gbp) {
result = inputAmount * (0.83 / 1.32);
Log.d("test", "Result is" + result);
txt.setText(""+result);
}
}
else {
Log.d("test", "nothing here");
}
if(radioId1 == R.id.inr){
if(radioId2 == R.id.usd){
result = inputAmount*68.14;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/68.14);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
}
else if(v.getId()== R.id.clearButton){
txt.setText("");
}
}
}
不幸的是,虽然一切似乎都正确但我无法让应用程序运行且应用程序崩溃。任何帮助解决这个问题表示赞赏。我是android的新手。
使用Logcat文件更新:
01-21 17:42:56.229 6017-6017/? I/art: Late-enabling -Xcheck:jni
01-21 17:42:56.244 6017-6023/? E/art: Failed sending reply to debugger: Broken pipe
01-21 17:42:56.244 6017-6023/? I/art: Debugger is no longer active
01-21 17:42:56.278 6017-6017/? W/System: ClassLoader referenced unknown path: /data/app/com.example.shara.currencyconvertor-1/lib/x86
01-21 17:42:56.278 6017-6017/? I/InstantRun: Instant Run Runtime started. Android package is com.example.shara.currencyconvertor, real application class is null.
01-21 17:42:56.526 6017-6017/? W/System: ClassLoader referenced unknown path: /data/app/com.example.shara.currencyconvertor-1/lib/x86
01-21 17:42:56.556 6017-6017/? D/AndroidRuntime: Shutting down VM
01-21 17:42:56.556 6017-6017/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shara.currencyconvertor, PID: 6017
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.shara.currencyconvertor/com.example.shara.currencyconvertor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.shara.currencyconvertor.MainActivity.<init>(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-21 17:47:56.701 6017-6017/com.example.shara.currencyconvertor I/Process: Sending signal. PID: 6017 SIG: 9
LogCat after making changes:
01-21 18:33:57.523 11590-11590/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shara.currencyconvertor, PID: 11590
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shara.currencyconvertor/com.example.shara.currencyconvertor.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.shara.currencyconvertor.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您在加载onCreate之前初始化视图。
,这是有道理的。你有理由得到NullPointerException。在此处尝试更改。请注意,所有视图都在顶部声明,但在OnCreate中初始化。 &#34; inputText&#34;不能是最终的,除非你在onCreate中声明它。
package com.example.shara.currencyconvertor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView txt;
private RadioGroup grp1;
private RadioGroup grp2;
private EditText inputAmt;
private String inputText;
private double inputAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.convertButton).setOnClickListener(this);
findViewById(R.id.clearButton).setOnClickListener(this);
txt = (TextView) findViewById(R.id.textView3);
grp1 = (RadioGroup) findViewById(R.id.radioGroup2);
grp2 = (RadioGroup) findViewById(R.id.radioGroup3);
inputAmt = (EditText) findViewById(R.id.inputAmount);
inputText = inputAmt.getText().toString();
inputAmount = Integer.parseInt(inputText);
grp1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton) findViewById(checkedId);
}
});
grp2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn2 = (RadioButton) findViewById(checkedId);
}
});
}
@Override
public void onClick(View v) {
int radioId1 = grp1.getCheckedRadioButtonId();
int radioId2 = grp2.getCheckedRadioButtonId();
double result = 0;
if(v.getId()==R.id.convertButton){
if(radioId1 == R.id.aud){
if(radioId2 == R.id.usd){
result = inputAmount*1.34;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/1.34);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
if(radioId1 == R.id.cad){
if(radioId2 == R.id.usd){
result = inputAmount*1.32;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if (radioId2 == R.id.gbp) {
result = inputAmount * (0.83 / 1.32);
Log.d("test", "Result is" + result);
txt.setText(""+result);
}
}
else {
Log.d("test", "nothing here");
}
if(radioId1 == R.id.inr){
if(radioId2 == R.id.usd){
result = inputAmount*68.14;
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else if(radioId2 == R.id.gbp){
result = inputAmount*(0.83/68.14);
Log.d("test", "Result is" +result);
txt.setText(""+result);
}
else {
Log.d("test", "nothing here");
}
}
}
else if(v.getId()== R.id.clearButton){
txt.setText("");
}
}
}
<强>更新强>
抱歉,我刚才注意到了XML。
如果EditText的内容为空,则需要解析它。你需要在使用后写一些东西来解析它。如果您不想添加按钮,可以使用:
inputAmt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
inputText = inputAmt.getText().toString();
inputAmount = Integer.parseInt(inputText);
}
@Override
public void afterTextChanged(Editable s) {
}
});