2011-04-02 71 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文件,而不是一個視圖對象,因爲你在這裏。

我會解決這個問題,分解成一系列更簡單的步驟。首先,在屏幕上並在正確的位置獲取所需的所有視圖。

然後編寫按鈕事件的處理程序 - 使其更改按鈕上的標籤。

最後加入計算。

不要咬牙切切,你可以咀嚼 - 成功編程的祕訣。

相關問題