2011-04-02 72 views
0

该应用程序应计算贷款支付的大小,并在按下按钮时打印答案,但是当按下按钮时,它会显示以下内容:java.lang.IllegalStateException :指定的孩子已经有父母。您必须先调用子对象的父对象的removeView()。您必须首先调用孩子父母的removeView()

这里的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical"> 
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/kokota"></EditText> 
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/prota"></EditText> 
     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lainata"></EditText> 
    <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/maksuta"></EditText> 

    <Button android:layout_width="match_parent" android:text="Laske!" android:layout_height="70dip" android:id="@+id/btnClickMe"></Button> 
    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/tasaera"></TextView> 
</LinearLayout> 

,代码:

package com.example.lainalaskuri; 

import java.text.DecimalFormat; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.EditText; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Toast; 

public class Lainalaskuri extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button btn1 = (Button)findViewById(R.id.btnClickMe); 
     btn1.setOnClickListener(btnListener); 
    } 

    double N, p, m, n, l, B; 

    public static double laskeTasaera(double p, double n, double m, double N) { 
     double A = (Math.pow(1 + p /(100 * m),n) * (p/(100* m)))/
     (Math.pow(1 + p /(100*m), n) -1) * N; 
     return A; 
    } 

    private OnClickListener btnListener = new OnClickListener() 
    { 
     public void onClick(View v) 
     { 

      EditText kokota, prota, lainata, maksuta; 
      String koko, pro, laina, maksu; 

      kokota = (EditText)findViewById(R.id.kokota); 
      prota = (EditText)findViewById(R.id.prota); 
      lainata = (EditText)findViewById(R.id.lainata); 
      maksuta = (EditText)findViewById(R.id.maksuta); 

      koko = kokota.getText().toString(); 
      int N = Integer.parseInt(koko);  

      pro = prota.getText().toString(); 
      double p = Double.parseDouble(pro); 

      laina = lainata.getText().toString(); 
      int l = Integer.parseInt(laina); 

      maksu = maksuta.getText().toString(); 
      int m = Integer.parseInt(maksu); 

      n = l * m; 

      B = laskeTasaera(p, n, m, N); 

      TextView tasaera = (TextView) findViewById(R.id.tasaera); 
       tasaera.setText((B) + " €"); 
       setContentView(tasaera);   
     } 
    }; 

} 

回答

0

到的setContentView的参数通常是一个布局的XML文件,而不是一个视图对象,因为你在这里。

我会解决这个问题,分解成一系列更简单的步骤。首先,在屏幕上并在正确的位置获取所需的所有视图。

然后编写按钮事件的处理程序 - 使其更改按钮上的标签。

最后加入计算。

不要咬牙切切,你可以咀嚼 - 成功编程的秘诀。

相关问题