簡體   English   中英

您必須先在孩子的父母上調用removeView()

[英]You must call removeView() on the child's parent first

該應用程序應該計算貸款的大小並在按下按鈕時打印答案,但是當我按下按鈕時,它將給出以下信息:java.lang.IllegalStateException:指定的孩子已經有一個父母。 您必須先在孩子的父母上調用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);        
        }
    };

}

setContentView的參數通常是布局XML文件,而不是View對象(如您在此處)。

我將通過分解為一系列更簡單的步驟來解決此問題。 首先,在正確的位置在屏幕上獲取所需的所有視圖。

然后為按鈕事件編寫一個處理程序-使它更改按鈕上的標簽。

最后添加計算。

不要咬得太多,這就是成功編程的秘訣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM