Search code examples
androidmain-activity

Not getting answer the percentage of love calculator value in textbox


Not getting answer the percentage of love calculator value in text box 3 .when the application running it is showing the error application stopped. The following is the code for that which is entered in mainactivity .

EditText t1;
EditText t2;
EditText t3;
Button b1;
int sum=0;
int sum2=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   Button b1=(Button)findViewById(R.id.button1);

   final TextView t1=(TextView)findViewById(R.id.textView1);
   final EditText t2=(EditText)findViewById(R.id.editText2);
   final EditText t3=(EditText)findViewById(R.id.editText3);

   t1.setText("");
   t2.setText("");

   b1.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        String s1=t1.getText().toString();

        String s2=t2.getText().toString();
        for (int i = 0; i < t1.length(); i++) {
            char ch= s1.charAt(i);
            int ascii=ch;
            sum=sum+ascii;
        }

        for (int i = 0; i <t2.length(); i++) {
            char ch= s1.charAt(i);
            int ascii=ch;               
            sum2=sum2+ascii;    
        }
        int total=sum+sum2;
        int lovepercentage=total%100;
        t3.setText(""+lovepercentage);

        }   
    });       
}

Xml code is bellow

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.lovecalculator.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="46dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="46dp"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="18dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Button" />

logcat message 11-05 19:56:11.554: W/dalvikvm(678): VFY: unable to resolve virtual method 406: Landroid/content/res/TypedArray;.getChangingConfigurations ()


Solution

  • You should simply change your variables to the right forms... Is there confusion between EditText and TextView?

    Button b1=(Button)findViewById(R.id.button1);
    
    final TextView viewLove = (TextView) findViewById(R.id.textView1)
    final EditText t1=(EditText)findViewById(R.id.editText1); //<----- here was your error!
    final EditText t2=(EditText)findViewById(R.id.editText2);
    // final EditText t3=(EditText)findViewById(R.id.editText3); <--- no need for this.
    

    move the TextView in your xml below EditText editText3, and in your onClick:

        int total=sum+sum2;
        int lovepercentage=total%100;
        viewLove.setText(""+lovepercentage);