我需要在每个条件下在MonthsTxtView中显示24,36,60,如果用户检查相应的单选按钮时。例如,如果用户检查2年我需要在MonthsTxtview上显示24等等...下面是我的代码但是当我正在运行并且检查单选按钮告诉我“不幸的是,Carloan(app的名称)已停止”并关闭我的应用程序有什么建议吗?
XML CODE:
<?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="com.example.ioann_000.carloan.MainActivity"
android:background="#E1E1E1"
>
<TextView
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/MonthsTxtView"
android:layout_marginBottom="48dp"
android:layout_above="@+id/Total2TxtView"
android:layout_alignStart="@+id/Total2TxtView"
android:clickable="false"
android:background="@drawable/editetxt_second" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
android:id="@+id/RadioGroup1"
android:layout_below="@+id/LoadurationTextView"
android:layout_alignStart="@+id/InsertTextView">
<RadioButton android:id="@+id/radioyear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 Years"
android:onClick="onRadioButtonClicked"
android:layout_alignTop="@+id/RadioGroup1"
android:layout_toStartOf="@+id/RadioGroup1" />
<RadioButton android:id="@+id/radioyear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 Years"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radioyear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5 Years"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
JAVA CODE:
package com.example.ioann_000.carloan;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "CARLOAN";
private TextView MonthsTxtView;
private TextView Total2TxtView;
private RadioGroup RadioGroup1;
private RadioButton selected;
private int years2=24;
private int years3=36;
private int years5=60;
private double MontlyI= 0;
private double loan = 0;
private RadioButton radioyear2;
private RadioButton radioyear3;
private RadioButton radioyear5;
private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
private static final NumberFormat percentFormat = NumberFormat.getPercentInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MonthsTxtView = (TextView) findViewById(R.id.MonthsTxtView);
Total2TxtView = (TextView) findViewById(R.id.Total2TxtView);
radioyear2 = (RadioButton) findViewById(R.id.radioyear2);
radioyear3 = (RadioButton) findViewById(R.id.radioyear3);
radioyear5 = (RadioButton) findViewById(R.id.radioyear5);
EditText IREditText = (EditText) findViewById(R.id.IREditText);
EditText AmountEditText = (EditText) findViewById(R.id.AmountEditText);
EditText AFEditText = (EditText) findViewById(R.id.AFEditText);
RadioBtn();
Log.i(TAG, "onCreate: ");
}
private void RadioBtn () {
RadioGroup selected= (RadioGroup) findViewById(R.id.RadioGroup1);
selected.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(radioyear2.isChecked()) {
MonthsTxtView.setText("24");
Log.i(TAG, "onCheckedChanged: ");
}
else if (radioyear3.isChecked()) {
MonthsTxtView.setText("36");
Log.i(TAG, "onCheckedChanged: ");
}
else
MonthsTxtView.setText("60");
Log.i(TAG, "onCheckedChanged: ");
}
});
Log.i(TAG, "RadioBtn: ");
}
}
单击单选按钮时出现错误:
**12-07 01:31:00.863 4006-4006/com.example.ioann_000.carloan E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ioann_000.carloan, PID: 4006
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
at com.example.ioann_000.carloan.MainActivity$1.onCheckedChanged(MainActivity.java:64)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174)
at android.widget.RadioGroup.access$600(RadioGroup.java:54)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358)
at android.widget.CompoundButton.setChecked(CompoundButton.java:157)
at android.widget.CompoundButton.toggle(CompoundButton.java:113)
at android.widget.RadioButton.toggle(RadioButton.java:78)
at android.widget.CompoundButton.performClick(CompoundButton.java:118)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
**
错误2:E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ioann_000.carloan, PID: 11445
java.lang.IllegalStateException: Could not find method onRadioButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'radioyear2'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Disconnected from the target VM, address: 'localhost:8625', transport: 'socket'
答案 0 :(得分:0)
您的一个RadioButton位于RadioGroup标记之外。先检查一下。此致!
你能做的另一件事就是吼叫:
int checkedRadioButtonId = selected.getCheckedRadioButtonId();
if (checkedRadioButtonId == -1) {
// No item selected
}
else{
switch(checkedRadioButtonId){
case R.id.radioyear2:
// Do something cool here
break;
case R.id.radioyear3:
// Do something cool here
break;
case R.id.radioyear5:
// Do something cool here
break;
}
}
而且,您错过了使用findViewById初始化MonthsTxtView ...
答案 1 :(得分:0)
看起来您没有初始化单选按钮。您需要使用findViewById将它们绑定到变量。如果您发布了堆栈跟踪
,它也会有所帮助在onCreate
radioyear2 = (RadioButton) findViewById(R.id.radioyear2);
radioyear3 = (RadioButton) findViewById(R.id.radioyear3);
radioyear5 = (RadioButton) findViewById(R.id.radioyear5);
MonthsTxtView = (TextView) findViewById(R.id.MonthsTxtView);
答案 2 :(得分:0)
最后我必须做这个改变:
`public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radioyear2:
if (checked)
MonthsTxtView.setText("24");
Log.i(TAG, "onRadioButtonClicked: ");
break;
case R.id.radioyear3:
if (checked)
MonthsTxtView.setText("36");
Log.i(TAG, "onRadioButtonClicked: ");
break;
case R.id.radioyear5:
if (checked)
MonthsTxtView.setText("60");
Log.i(TAG, "onRadioButtonClicked: ");
break;
}
}
}`
感谢@David Rawson