0
votes

Whenever i click on Submit button, i get an error "Unfortunately, Application has stopped working." Please can you help me out.

Code-

    public void onClick(View v) {
            TextView t = (TextView) findViewById(R.id.textView4);   
            if(from == to)
            {
                Toast.makeText(getApplicationContext(), "Invalid", 4000).show();

            }


            else
            {                                       
                  try {
                     s = getJson("URL");                        
                    JSONObject jObj;
                    jObj = new JSONObject(s);
                    String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
                    t.setText(exResult);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }                                                    
                }   

            }                               
    });
}

LogCat -

03-15 12:06:17.508: E/AndroidRuntime(877): FATAL EXCEPTION: main 03-15 12:06:17.508: E/AndroidRuntime(877): android.os.NetworkOnMainThreadException 03-15 12:06:17.508: E/AndroidRuntime(877): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 03-15 12:06:17.508: E/AndroidRuntime(877): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 03-15 12:06:17.508: E/AndroidRuntime(877): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 03-15 12:06:17.508: E/AndroidRuntime(877): at java.net.InetAddress.getAllByName(InetAddress.java:214) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 03-15 12:06:17.508: E/AndroidRuntime(877): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 03-15 12:06:17.508: E/AndroidRuntime(877): at myandroid.myfirstmodule.sconverter.getJson(sconverter.java:163) 03-15 12:06:17.508: E/AndroidRuntime(877): at myandroid.myfirstmodule.sconverter$1.onClick(sconverter.java:96) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.view.View.performClick(View.java:4084) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.view.View$PerformClick.run(View.java:16966) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.os.Handler.handleCallback(Handler.java:615) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.os.Handler.dispatchMessage(Handler.java:92) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.os.Looper.loop(Looper.java:137) 03-15 12:06:17.508: E/AndroidRuntime(877): at android.app.ActivityThread.main(ActivityThread.java:4745) 03-15 12:06:17.508: E/AndroidRuntime(877): at java.lang.reflect.Method.invokeNative(Native Method) 03-15 12:06:17.508: E/AndroidRuntime(877): at java.lang.reflect.Method.invoke(Method.java:511) 03-15 12:06:17.508: E/AndroidRuntime(877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 03-15 12:06:17.508: E/AndroidRuntime(877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 03-15 12:06:17.508: E/AndroidRuntime(877): at dalvik.system.NativeStart.main(Native Method)

3
what is the problem when clicking the second button ?William Kinaan
I don't think placing submitfinal's onClick inside button b's onCLick is a right thing to do. Why do you need to do that?kdroider
What are you trying to achieve...?Pragnani
Whenever i click on second button, i m automatically moved to the previous page.user1603198
ok..i m first fetching the data using yahoo api(when clicked first time..works well) and then manipulating that data to multiply it with text entered by the user in the textbox(by clicking on second button...automatically moved to previous page)user1603198

3 Answers

0
votes

Why don't you implement the onclickevent in the layout like this:

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Here"
    android:onClick="btnClicked" />

Then define the method btnClicked(). I think that way you can use this onClickListener with a custom method. You can even use the same method for the button1 and inside of it determine which button has been called.

0
votes

You might try to move the listener of the second button from the else block. I think it is not a good practice to place a listener inside another listener

0
votes

I am not sure what exactly is your goal in your code. But I don't think placing submitfinal's onClick inside button b's onCLick is a right thing to do. You need to handle them separately. If you want that in one onClickListener, you can do:

  int x, y, product; 
  public OnClickListener buttonClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
                switch (v.getId()) {

            case <button b id>: 
                //fetch values of x and y
                                    x = Integer.parseInt(yourtextbox1.getText().toString());
                                    y = Integer.parseInt(yourtextbox2.getText().toString());
            break;
            case <button submit final id>: 
                //multiply x and y
                       product = x*y;
                    //display the product
                       yourtextbox3.setText(product);
            break;
            }
     };

 b.setOnClickListener(buttonClickListener);
 submitFinal.setOnClickListener(buttonClickListener);

EDIT:

On your current code, move the lines:

 t = (TextView) findViewById(R.id.textView4);    
 displayfinal = (TextView) findViewById(R.id.textView3);

outside your onclick listener.

Hope that helps.