1
package kdev.circles;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText ca = (EditText)findViewById(R.id.ca);
        final EditText cp = (EditText)findViewById(R.id.cp);
        final EditText sv = (EditText)findViewById(R.id.sv);
        final EditText r = (EditText)findViewById(R.id.radius);
        final TextView log = (TextView)findViewById(R.id.log);
        Button clear = (Button)findViewById(R.id.clear);
        Button solve = (Button)findViewById(R.id.Solve);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ca.setText("");
                cp.setText("");
                sv.setText("");
                r.setText("");
            }
        });
        solve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if((r.toString()).equals("")) {
                    log.setText(log.getText() + "\n" + "error, no value entered");
                }else{
                    sv.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * Double.valueOf(String.valueOf(r.getText())) * 4 / 3 * 3.14159265));
                    cp.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*3.14159265*2));
                    ca.setText(String.valueOf(Double.valueOf(String.valueOf(r.getText()))*Double.valueOf(String.valueOf(r.getText()))*3.14159265));
                    log.setText(log.getText()+"\n"+"ca"+ca.getText()+"\n"+"sv"+sv.getText()+"\n"+"cp"+cp.getText());
                }
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

xml file:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Solve"
    android:id="@+id/Solve"
    android:width="100dp"
    android:layout_alignParentBottom="true"
    android:layout_alignEnd="@+id/sv" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/radius"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/editText" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="49dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_below="@+id/editText"
    android:layout_alignStart="@+id/editText" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_below="@+id/editText2"
    android:layout_alignStart="@+id/editText2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="circle&apos;s perimeter"
    android:id="@+id/cp"
    android:layout_above="@+id/editText2"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="circle&apos;s area"
    android:id="@+id/ca"
    android:layout_above="@+id/editText3"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="sphere&apos;s volume"
    android:id="@+id/sv"
    android:layout_alignBottom="@+id/editText3"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="radius"
    android:id="@+id/textView3"
    android:layout_alignBottom="@+id/radius"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="clear"
    android:id="@+id/clear"
    android:layout_alignBottom="@+id/Solve"
    android:layout_alignStart="@+id/editText3" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/log"
    android:layout_below="@+id/editText3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="57dp" />

Everything is working fine till i try opening it, i couldn't find the error, because my android virtual device wasn't connected to android studio properly. I am not sure what i should do. I did have another problem before but i realised that it was due to me using the findviewbyid function before the whole layout was loaded. I have solved that problem and tried a few different techniques to solve this problem but nothing worked. When i try and run the app it just crashes, i am not sure why? Any help will be usefull, thank you. this is the logcat:

12-10 15:21:18.030    1907-1907/kdev.circles W/art﹕ Verification of java.lang.CharSequence android.support.v7.widget.ResourcesWrapper.getQuantityText(int, int) took 123.881364ms
12-10 15:21:18.520    1907-1907/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:18.520    1907-1907/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: kdev.circles, PID: 1907
    java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
     Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
            at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-10 15:21:27.230    1907-1907/kdev.circles I/Process﹕ Sending signal. PID: 1907 SIG: 9
12-10 15:21:31.360    2153-2153/kdev.circles D/AndroidRuntime﹕ Shutting down VM
12-10 15:21:31.360    2153-2153/kdev.circles E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: kdev.circles, PID: 2153
    java.lang.RuntimeException: Unable to start activity ComponentInfo{kdev.circles/kdev.circles.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
     Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
            at kdev.circles.MainActivity.onCreate(MainActivity.java:21)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
K Dev
  • 25
  • 6

1 Answers1

2

Check your view types in Java code and their matching id's in xml. They have to be of same type.

You have EditText in Java, but matching view id in xml is TextView

For instance:

final EditText ca = (EditText)findViewById(R.id.ca);

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="circle&apos;s area"
    android:id="@+id/ca"
    android:layout_above="@+id/editText3"
    android:layout_alignParentStart="true" />
Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
  • Thank you ever so much this did answer my question, it seems i had labelled all the textviews what i should have labelled EditTexts, now i feel a bit stupid, but thanks anyways for the help. – K Dev Dec 10 '15 at 20:16
  • I have actually changed everything to make sure all the ID's are set correctly but it isn't working, still crashing. – K Dev Dec 10 '15 at 20:24
  • LogCat you have posted confirms that you have mismatched types. If you have assigned proper id's to proper views, your code should work. If it is not working, then something else is wrong and you need to look at new LogCat and read the errors - it will point you to your failing code just like it did point to line 21 in your MainActivity the first time. – Dalija Prasnikar Dec 10 '15 at 21:02