for some reason, I am getting a ClassCastException error. I don't really know why.
Thanks in advance, I am a new programmer and any help would really help (no pun intended).
Code:
final EditText answerBox = (EditText) findViewById(R.id.answerBox);
final Button button = (Button) findViewById(R.id.button);
final TextView problem = (TextView) findViewById(R.id.problem);
final TextView status = (TextView) findViewById(R.id.status);
final TextView num = (TextView) findViewById(R.id.numerator);
final TextView denom = (TextView) findViewById(R.id.denominator);
button.setOnClickListener(new OnClickListener()
{
private String inputString;
public void onClick(View v)
{
inputString = problem.getText().toString();
int firstNumber = Integer.parseInt(inputString.substring(0,1));
int secondNumber = Integer.parseInt(inputString.substring(2,3));
int correct = firstNumber + secondNumber;
int input;
if(!answerBox.getText().toString().equals(""))
input = Integer.parseInt(answerBox.getText().toString());
else
input = -1;
if(input != -1)
{
if(input == correct)
{
status.setText("Nice! You are correct!");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
num.setText(Integer.parseInt(num.getText().toString()) +1);
}
else
{
status.setText("Sorry, but your answer was wrong.");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
}
}
int a = (int) (Math.random() * 10);
int b = (int) (Math.random() * 10);
String newProblem = Integer.toString(a) + "+" + Integer.toString(b);
problem.setText(newProblem.toString());
answerBox.setText("");
}
});
Once again, thanks.
Stuff from LogCat:
10-30 04:04:10.490: ERROR/AndroidRuntime(551): FATAL EXCEPTION: main
10-30 04:04:10.490: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.benhsu.Addition/com.benhsu.Addition.AdditionActivity}: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:507)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.benhsu.Addition.AdditionActivity.onCreate(AdditionActivity.java:24)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): ... 11 more
XML Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:id="#+id/problem" android:layout_height="wrap_content" android:text="#string/problemString"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/answerBox">
<requestFocus></requestFocus>
</EditText>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/status" android:id="#+id/status"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:id="#+id/button" android:layout_width="wrap_content"></Button>
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="0.14">
<TextView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_alignBottom="#+id/textView1" android:text="#string/score" android:id="#+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignBaseline="#+id/textView1"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/numerator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/textView2" android:layout_marginLeft="18dp" android:text="#string/numerator"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/fraction" android:id="#+id/fraction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/numerator" android:layout_marginLeft="14dp"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/denominator" android:id="#+id/denominator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/fraction" android:layout_marginLeft="19dp"></TextView>
</RelativeLayout>
</LinearLayout>
EDIT
It worked fine for me. In Eclipse, go to Project->Clean... and try again. It could be that your R file is out of sync.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am new on android. I have just made my first app. But as I run my my app on android emulator, it does not run. App compiled without any error, but on emulator it's showing that unfortunately app is stopped.
This is the java code:
package com.example.kartikeya;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
double x,y;
static int op=0;
double result;
TextView textView = new TextView(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this); // calling onClick() method
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
//Button button3 = (Button) findViewById(R.id.button3);
// button3.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(this);
}
private void setContentView() {
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Button button = new Button(this);
button.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 9);
break;
case R.id.button2:
Button button2 = new Button(this);
button2.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 8);
break;
case R.id.button3:
Button button3 = new Button(this);
button3.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 7);
break;
case R.id.button4:
Button button4 = new Button(this);
button4.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 6);
break;
case R.id.button5:
Button button5 = new Button(this);
button5.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 5);
break;
case R.id.button6:
Button button6 = new Button(this);
button6.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 4);
break;
case R.id.button7:
Button button7 = new Button(this);
button7.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 3);
break;
case R.id.button8:
Button button8 = new Button(this);
button8.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 2);
break;
case R.id.button9:
Button button9 = new Button(this);
button9.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 1);
break;
case R.id.button10:
Button button10 = new Button(this);
button10.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + 0);
break;
case R.id.button11:
Button button11 = new Button(this);
button11.setBackgroundColor(Color.CYAN);
textView.setText(textView.getText() +"" + '.');
break;
case R.id.button13:
x=Double.parseDouble((String)textView.getText());
op=1;
Button button13 = new Button(this);
button13.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button14:
x=Double.parseDouble((String)textView.getText());
op=2;
Button button14 = new Button(this);
button14.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button15:
x=Double.parseDouble((String)textView.getText());
op=3;
Button button15 = new Button(this);
button15.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button16:
x=Double.parseDouble((String)textView.getText());
op=4;
Button button16 = new Button(this);
button16.setBackgroundColor(Color.CYAN);
textView.setText(" ");
break;
case R.id.button12:
y=Double.parseDouble((String) textView.getText());
Button button12 = new Button(this);
button12.setBackgroundColor(Color.CYAN);
switch(op)
{
case 1:
{
result=x+y;
break;
}
case 2:
{
result=x-y;
break;
}
case 3:
{
result=x*y;
break;
}
case 4:
{
result=x/y;
break;
}
default :
break;
}
if(result-Math.floor(result)==0)
{
result=Math.floor(result);
}
textView.setText(result+"");
break;
default:
break;
}
}
}
This is xml file
<?xml version="1.0" encoding="utf-8"?>
<Button
android:text="#string/button0"
android:id="#+id/button"
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="#string/click1"
android:contextClickable="true" />
<TextView
android:layout_width="350dp"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/text1"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffffff" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_marginLeft="2.5dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button7"
android:id="#+id/button3"
android:layout_alignTop="#+id/button13"
android:layout_toRightOf="#+id/button5" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button6"
android:id="#+id/button4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button5"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:layout_marginLeft="2.5dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button4"
android:id="#+id/button6"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button3"
android:id="#+id/button7"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button10"
android:id="#+id/button8"
android:layout_below="#+id/button5"
android:layout_alignLeft="#+id/button5"
android:layout_alignStart="#+id/button5"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button11"
android:id="#+id/button9"
android:layout_alignTop="#+id/button15"
android:layout_toRightOf="#+id/button8" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button12"
android:id="#+id/button10"
android:layout_below="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button13"
android:id="#+id/button11"
android:layout_below="#+id/button9"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8"
android:layout_marginTop="10dp"/>
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button18"
android:id="#+id/button12"
android:layout_alignTop="#+id/button16"
android:layout_toRightOf="#+id/button11" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button14"
android:id="#+id/button13"
android:layout_above="#+id/button4"
android:layout_alignParentRight="true" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button15"
android:id="#+id/button14"
android:layout_alignTop="#+id/button6"
android:layout_alignParentRight="true" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button16"
android:id="#+id/button15"
android:layout_alignTop="#+id/button8"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<Button
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:text="#string/button17"
android:id="#+id/button16"
android:layout_below="#+id/button9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"/>
This is AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is the LogCat
12-21 22:15:23.620 1425-1425/com.example.kartikeya.calc D/dalvikvm: Not late-enabling CheckJNI (already on)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc D/AndroidRuntime: Shutting down VM
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb2fed180)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: FATAL EXCEPTION: main
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kartikeya.calc/com.example.kartikeya.calc.MainActivity}: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4424)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: Caused by: java.lang.NullPointerException
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.view.View.<init>(View.java:2696)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.view.View.<init>(View.java:2744)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:449)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc `E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:442)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.widget.TextView.<init>(TextView.java:437)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.example.kartikeya.calc.MainActivity.<init>(MainActivity.java:14)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.Class.newInstanceImpl(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1319)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4424)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-21 22:15:23.640 1425-1425/com.example.kartikeya.calc E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.140 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: threadid=3: reacting to signal 3
12-21 22:15:24.160 1425-1431/com.example.kartikeya.calc I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
12-21 22:15:27.801 1425-1425/? I/Process: Sending signal. PID: 1425 SIG: 9
Your problem is this line TextView textView = new TextView(this);
You are trying to call setText() on this view in your onClick methods, but this view hasn't been inflated. You have to inflate the view before you can manipulate it, just like you did with Button button = (Button) findViewById(R.id.button);
So in your onCreate() you would want to put in textView = (TextView) findViewById(R.id.your_text_view_id); and get rid of your TextView textView = new TextView(this); line -- just make it TextView textView;
I'm an android newbie
I was following this tutorial Android splash screen howto, it's a little outdated, but anyways.
I just add the new layout file splash.xml into res/layout, and then the image into drawable/mdpi, then I change the main image into MainActivity.java, ie:
setContentView(R.layout.activity_main);
to
setContentView(R.layout.splash);
Being activity_main the name of the default layout, activity_main.xml, and splash the name of the other layout called splash.xml.
But application stops, says the application has unfortunately stopped, and I can't even access it.
I don't know what am I missing here, here's the interesting code on MainActivity.java:
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)
findViewById(R.id.surfaceView1);
// cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
// cameraSurfaceHolder.setType(SurfaceHolder.
// SURFACE_TYPE_PUSH_BUFFERS);
btnCapture = (Button)findViewById(R.id.button1);
btnCapture.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
}
});
}
My activity_main.xml:
<FrameLayout 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"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/containerImg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="1276px"
android:layout_height="745px"
android:layout_centerInParent="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/surfaceView1"
android:layout_alignLeft="#+id/surfaceView1"
android:layout_marginLeft="20px"
android:src="#drawable/mark3" />
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:background="#drawable/camera" />
</FrameLayout>
My splash.xml:
<?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">
<ImageView
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
</LinearLayout>
Any ideas?
Maybe I should use the same activity_main.xml for this purposes, not really sure...
Thanks in advance!
EDIT
Logcat:
3774-3774/com.kkoci.photo E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kkoci.photo/com.kkoci.photo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.kkoci.photo.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
these are the views present in your activity_main xml file.. when you change your layout file why you are accessing these?
I am trying to make a simple android app that will add two numbers and display the output.However, I am getting the following error continuously.
"calculator.com.process stopped unexpectedly"
I used API 10 for rendering layout and API 17 as compiler and Target SDK.
Could post the code finally after all these trouble.
This is the code of main_activity.java
package com.example.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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 ActionBarActivity {
EditText txtNum1;
EditText txtNum2;
TextView Display;
Button bCal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNum1=(EditText) findViewById(R.id.Num1);
txtNum1=(EditText) findViewById(R.id.Num2);
Display=(TextView)findViewById(R.id.tvDisplay);
bCal=(Button)findViewById(R.id.bAdd);
bCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Tawfiq) {
// TODO Auto-generated method stub
double x=Double.parseDouble(txtNum1.getText().toString());
double y=Double.parseDouble(txtNum2.getText().toString());
double total=x+y;
Display.setText("Sum is"+total);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bAdd"
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.calculator.MainActivity" >
<EditText
android:id="#+id/Num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Num1"
android:layout_below="#+id/Num1"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/bAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Button" />
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Num2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Sum"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
This is the logcat.
01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130): ... 11 more
You are having problem at line no-27
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
Change the relativeLayouts Id in the xml file.
It should not be same as for the add button.
android:id="#+id/relativeLayout"
Change it as above.
Two id in the same layout cannot be same.
Your add button and the relative layout is having the same id,.
And during the findViewById(), relative layout is being caste in the Button.
That's why class cast exception is coming.
Edit:
I compiled your code:-
the next mistake you have made is-
txtNum1=(EditText) findViewById(R.id.Num2);
It should be
txtNum2=(EditText) findViewById(R.id.Num2);
Mistake-You are not assigning the reference to the second edittext.
I am new on android and trying to develop simple calculator but this Fatal exception : main occurs please help.
I did comment my onClickListner to check but it did'nt helped at all.
package com.example.calculatorsinglescreen;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText value1,value2,myoperator,result;
Button ok;
String strvalue1,strvalue2,stroperator,strresult;
int ivalue1,ivalue2,ioperator,iresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1 = (EditText) findViewById(R.id.txtvalue1);
value2 = (EditText) findViewById(R.id.txtvalue2);
myoperator = (EditText) findViewById(R.id.txtoperator);
result = (EditText) findViewById(R.id.txtresult);
ok = (Button) findViewById(R.id.btnok);
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (stroperator.equals("+")) {
Toast msg = Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG);
msg.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my XML file :
<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.calculatorsinglescreen.MainActivity" >
<TextView
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:text="Value 1" />
<TextView
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value1"
android:layout_margin="20dp"
android:text="Value 2" />
<TextView
android:id="#+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/value2"
android:layout_margin="20dp"
android:text="Operator" />
<EditText
android:id="#+id/txtvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/value1"
android:layout_alignBaseline="#+id/value1"
android:layout_alignParentRight="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtoperator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/operator"
android:layout_below="#+id/txtvalue2"
android:layout_toRightOf="#+id/operator"
android:layout_alignParentRight="true"
android:ems="10" />
<EditText
android:id="#+id/txtvalue2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/value2"
android:layout_toRightOf="#+id/value2"
android:layout_below="#+id/txtvalue1"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="#+id/btnok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Ok" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnok"
android:layout_margin="40dp"
android:text="Result" />
<EditText
android:id="#+id/txtresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/result"
android:layout_toRightOf="#+id/result"
android:ems="10" />
Here is my LogCat to get a clearer view :
10-18 17:51:25.468: D/AndroidRuntime(742): Shutting down VM
10-18 17:51:25.468: W/dalvikvm(742): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-18 17:51:25.488: E/AndroidRuntime(742): FATAL EXCEPTION: main
10-18 17:51:25.488: E/AndroidRuntime(742): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculatorsinglescreen/com.example.calculatorsinglescreen.MainActivity}: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.os.Looper.loop(Looper.java:123)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-18 17:51:25.488: E/AndroidRuntime(742): at dalvik.system.NativeStart.main(Native Method)
10-18 17:51:25.488: E/AndroidRuntime(742): Caused by: java.lang.NumberFormatException: unable to parse '' as integer
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:362)
10-18 17:51:25.488: E/AndroidRuntime(742): at java.lang.Integer.parseInt(Integer.java:332)
10-18 17:51:25.488: E/AndroidRuntime(742): at com.example.calculatorsinglescreen.MainActivity.onCreate(MainActivity.java:34)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 17:51:25.488: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-18 17:51:25.488: E/AndroidRuntime(742): ... 11 more
this is the error
java.lang.ClassCastException: android.widget.TextView
check your line 24 and see if you cast the right widget in java -(xml), this error means incompatible cast widget lol...
if everything seems correct-(mean widgets are cast in java to with their respective class) then clean rebuild and restart..
EDIT: It worked.. you've solved it but there is a new error which is
java.lang.NumberFormatException:
its because of this on these lines
ivalue1 = Integer.parseInt(strvalue1);
ivalue2 = Integer.parseInt(strvalue2);
ioperator = Integer.parseInt(stroperator);
so change these to this
Integer.valueOf(strvalue1); do that as follows
and also looking at your codes.. your string values are pullled from the edittext during oncreate which means when the app starts in oncreate before the Onresume(which is called when the app shows on the), and during oncreate the user cant flirt with your app, and going further to input values, so at the end your strings are empty when you pull from the edittext, so basically what im saying is remove these lines
strvalue1 = value1.getText().toString();
and put them in the click events ... i am lucid enough to you??..
EDIT2: OVERALL CODE
for button click
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
strvalue1 = value1.getText().toString();
strvalue2 = value2.getText().toString();
stroperator = myoperator.getText().toString();
ivalue1 = Integer.valueOf(strvalue1);
ivalue2 = Integer.valueOf(strvalue2);
if (stroperator.equals("+")) {
Toast.makeText(MainActivity.this, "If is running", Toast.LENGTH_LONG).show();
}
}
});
remove ioperator = Integer.parseInt(stroperator); from the code.I think Your trying to convert the operator to Integer.
ioperator = Integer.parseInt(stroperator);
if you mean operator like this +,-,/,* , you only can convert to thr char or stay in string and do something like this:
private int plus(int ivalue1 , int ivalue2 , String operator ){
if(operator.equal("+")){
return ivalue1 + ivalue2;
}
return 0;
}
I created a custom component class which is just the RadioGroup class changed to extend RelativeLayout instead of LinearLayout when I try to run my app I get this error:
06-16 00:01:17.573: ERROR/AndroidRuntime(567): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 00:01:17.573: ERROR/AndroidRuntime(567): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
Line 38 refers to this line:
lsRadioGroupRelative = (RadioGroupRelative) findViewById(R.id.radioGroupRelative1);
Where that variable is defined as:
private RadioGroupRelative lsRadioGroupRelative;
Here is the layout.xml file being used in this part of the app:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background_levelselect">
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.stickfigs.blockball.RadioGroupRelative
android:id="#+id/radioGroupRelative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="64px"
android:layout_marginTop="96px"
android:orientation="horizontal">
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton1"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton2"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="2"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton1"
android:layout_alignTop="#id/levelButton1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton3"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="3"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton2"
android:layout_alignTop="#id/levelButton2">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton4"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="4"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton1"
android:layout_alignLeft="#id/levelButton1">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton5"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="5"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton4"
android:layout_alignTop="#id/levelButton4">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton6"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="6"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton5"
android:layout_alignTop="#id/levelButton5">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton7"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="7"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton4"
android:layout_alignLeft="#id/levelButton4">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton8"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="8"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton7"
android:layout_alignTop="#id/levelButton7">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton9"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="9"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton8"
android:layout_alignTop="#id/levelButton8">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton10"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="10"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton7"
android:layout_alignLeft="#id/levelButton7">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton11"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="11"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton10"
android:layout_alignTop="#id/levelButton10">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton12"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="12"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton11"
android:layout_alignTop="#id/levelButton11">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton13"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="13"
android:layout_marginTop="32px"
android:layout_below="#id/levelButton10"
android:layout_alignLeft="#id/levelButton10">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton14"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="14"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton13"
android:layout_alignTop="#id/levelButton13">
</view>
<view
class="com.stickfigs.blockball.BlockBallLevelSelect$RadioButtonText"
android:button="#drawable/bb_button"
android:id="#+id/levelButton15"
android:layout_height="96px"
android:layout_width="96px"
android:textColor="#fff"
android:text="15"
android:layout_marginLeft="32px"
android:layout_toRightOf="#id/levelButton14"
android:layout_alignTop="#id/levelButton14">
</view>
</com.stickfigs.blockball.RadioGroupRelative>
<Button
android:id="#+id/levelButtonGo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="128px"
android:layout_marginTop="22px"
android:layout_below="#id/radioGroupRelative1"
android:text=""
android:background="#drawable/level_select_button_play"
android:onClick="goClicked"/>
</RelativeLayout>
</FrameLayout>
I'm not sure what is causing the error since I'm not trying to cast it to something it is not, what is going on?
UPDATE: Constructors:
/**
* {#inheritDoc}
*/
public RadioGroupRelative(Context context) {
super(context);
//setOrientation(VERTICAL);
init();
}
/**
* {#inheritDoc}
*/
public RadioGroupRelative(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes = context.obtainStyledAttributes(
attrs, R.styleable.RadioGroupRelative, 0,
android.R.style.Widget_CompoundButton_RadioButton);
int value = attributes.getResourceId(R.styleable.RadioGroupRelative_android_checkedButton,
View.NO_ID);
if (value != View.NO_ID) {
mCheckedId = value;
}
attributes.recycle();
init();
}
private void init() {
mChildOnCheckedChangeListener = new CheckedStateTracker();
mPassThroughListener = new PassThroughHierarchyChangeListener();
super.setOnHierarchyChangeListener(mPassThroughListener);
}
UPDATE More stacktrace:
06-16 02:18:13.814: INFO/AndroidRuntime(638): NOTE: attach of thread 'Binder Thread #3' failed
06-16 02:18:13.964: INFO/ActivityManager(58): Start proc com.stickfigs.blockball for activity com.stickfigs.blockball/.BlockBallLevelSelect: pid=645 uid=10036 gids={}
06-16 02:18:14.584: DEBUG/AndroidRuntime(645): Shutting down VM
06-16 02:18:14.584: WARN/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): FATAL EXCEPTION: main
06-16 02:18:14.654: ERROR/AndroidRuntime(645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.blockball/com.stickfigs.blockball.BlockBallLevelSelect}: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.os.Looper.loop(Looper.java:123)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at java.lang.reflect.Method.invoke(Method.java:521)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at dalvik.system.NativeStart.main(Native Method)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): Caused by: java.lang.ClassCastException: com.stickfigs.blockball.RadioGroupRelative
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at com.stickfigs.blockball.BlockBallLevelSelect.onCreate(BlockBallLevelSelect.java:38)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-16 02:18:14.654: ERROR/AndroidRuntime(645): ... 11 more
06-16 02:18:14.786: WARN/ActivityManager(58): Force finishing activity com.stickfigs.blockball/.BlockBallLevelSelect
06-16 02:18:15.326: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44ed9600 com.stickfigs.blockball/.BlockBallLevelSelect}
06-16 02:18:19.254: INFO/Process(645): Sending signal. PID: 645 SIG: 9
06-16 02:18:19.294: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#44ea5ab8
06-16 02:18:19.584: INFO/ActivityManager(58): Process com.stickfigs.blockball (pid 645) has died.
06-16 02:18:20.106: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
06-16 02:18:20.783: DEBUG/dalvikvm(217): GC_EXPLICIT freed 100 objects / 4192 bytes in 125ms