jackie chen jackie chen - 1 month ago 8
Android Question

AndroidStudios onClick Error

Hi Im new to androidStudios but this is probably really simple but im not sure why this doesn't work.

this is the error coming out:

java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610) 
at android.view.View$PerformClick.run(View.java:22260) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5
at android.content.res.Resources.getText(Resources.java:331)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4550)
at jackiec.downloadmanagerapp.MainActivity.print(MainActivity.java:44)
at jackiec.downloadmanagerapp.MainActivity.add(MainActivity.java:38)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5610) 
at android.view.View$PerformClick.run(View.java:22260) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 


activty_Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="jackiec.downloadmanagerapp.MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:text="enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Enter"
android:onClick="add"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Please enter a download speed:"
android:ems="10"
android:id="@+id/speed"
android:layout_below="@+id/server"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter the probability of new Regular job:"
android:ems="10"
android:layout_below="@+id/probP"
android:id="@+id/probR"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<EditText
android:layout_width="match_parent"
android:inputType="textPersonName"
android:text="enter a probability of new premium job:"
android:ems="10"
android:id="@+id/probP"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="18sp"
android:layout_below="@+id/length"
android:layout_height="wrap_content" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Enter the number of servers:"
android:ems="10"
android:id="@+id/server"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text='Please enter a length of time:'
android:ems="10"
android:id="@+id/length"
android:layout_below="@+id/speed"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:text="Ouput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Output"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_below="@+id/Enter" />
</LinearLayout>

</RelativeLayout>


MainActivty.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output= (TextView) this.findViewById(R.id.Output);
server =(EditText) this.findViewById(R.id.server);
length = (EditText) this.findViewById(R.id.length);
speed = (EditText) this.findViewById(R.id.speed);
probR = (EditText) this.findViewById(R.id.probR);
probP = (EditText)this.findViewById(R.id.probP);
}
TextView output;
EditText server;
EditText length;
EditText speed;
EditText probP;
EditText probR;

public void add(View view)
{

int nServer= Integer.parseInt(server.getText().toString());
int nLength = Integer.parseInt(length.getText().toString());
int nSpeed = Integer.parseInt(speed.getText().toString());
int p= Integer.parseInt(probP.getText().toString());
int r = Integer.parseInt(probR.getText().toString());
int addAll = nServer+nLength+nSpeed+p+r;
print(addAll);


}
public void print(int a)
{
output.setText(a);
}



}


I thought it was maybe it has something wrong doing Integer.parse().
Thanks for the help.

Answer

For all EditText you had set the text. Try to use hint instead of text for all edittexts

android:text="Enter the number of servers:"

to

android:hint="Enter the number of servers:"

and put all Interger parsing inside try catch block

try {
  int nServer= Integer.parseInt(server.getText().toString());
    int nLength = Integer.parseInt(length.getText().toString());
    int nSpeed = Integer.parseInt(speed.getText().toString());
    int p= Integer.parseInt(probP.getText().toString());
    int r = Integer.parseInt(probR.getText().toString());
    int addAll = nServer+nLength+nSpeed+p+r;
    print(addAll);
} catch (NumberFormatException e) {
   //alert the user that the number they tried to enter was invalid
 }