1

hi i have developed a app to post data from android to website.

i got issue android.os.NetworkOnMainThreadException then i use AsyncTask to over come this issue as suggested here in my previous thread (How To Post Data From An Android App To a Website)

now when i run app i got message Unfortunately app has stopped an then app closed. where is the issue now??

package com.latlongapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

    Button sendButton;

    EditText msgTextField;

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

         // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
    }

    class SendTask extends AsyncTask<String, Void, String> {
        @Override
            protected String doInBackground(String... params) {
                  // get the message from the message text box
                String msg = msgTextField.getText().toString();  

                // make sure the fields are not empty
                if (msg.length()>0)
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
                 try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                   nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                   nameValuePairs.add(new BasicNameValuePair("message", msg));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   httpclient.execute(httppost);
                   msgTextField.setText(""); // clear text box
                 } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                 }

                }
                else
                {
                    // display message if text fields are empty
                    Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
                }
             return null;
        }

     public void send(View v)
        {
         new SendTask().execute();
        }

}}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
    android:text="Message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    /> 

<EditText
    android:id="@+id/msgTextField"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:text="Send"
    android:id="@+id/sendButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="send"
    /> 

manifest

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


        <uses-permission android:name="android.permission.INTERNET" /> 



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.latlongapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

PHP SCript being used is

<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>

LOGCAT

07-19 00:37:11.148: W/dalvikvm(23394): threadid=1: thread exiting with uncaught exception (group=0x410ac9a8)
07-19 00:37:11.168: E/AndroidRuntime(23394): FATAL EXCEPTION: main
07-19 00:37:11.168: E/AndroidRuntime(23394): java.lang.IllegalStateException: Could not find a method send(View) in the activity class com.latlongapp.MainActivity for onClick handler on view class android.widget.Button with id 'sendButton'
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$1.onClick(View.java:3594)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View.performClick(View.java:4212)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$PerformClick.run(View.java:17476)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Handler.handleCallback(Handler.java:800)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Handler.dispatchMessage(Handler.java:100)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.os.Looper.loop(Looper.java:194)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.app.ActivityThread.main(ActivityThread.java:5371)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.reflect.Method.invokeNative(Native Method)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.reflect.Method.invoke(Method.java:525)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at dalvik.system.NativeStart.main(Native Method)
07-19 00:37:11.168: E/AndroidRuntime(23394): Caused by: java.lang.NoSuchMethodException: send [class android.view.View]
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at java.lang.Class.getMethod(Class.java:915)
07-19 00:37:11.168: E/AndroidRuntime(23394):    at android.view.View$1.onClick(View.java:3587)
07-19 00:37:11.168: E/AndroidRuntime(23394):    ... 11 more
Community
  • 1
  • 1
user2590541
  • 522
  • 3
  • 7
  • 19

2 Answers2

3

send method need to be the method of MainActivity. your code isn't.

Mitsuaki Ishimoto
  • 3,162
  • 2
  • 25
  • 32
  • 2
    More specifically, instead of being a method of the Activity class it's a method of the inner AsyncTask class as the bracket closing the inner class comes after it rather than before. Moving the bracket should fix it. – Chris Stratton Jul 18 '14 at 19:55
0

The issue is you have defined a onClick attribute for the Button in your layout. But there is no implementation in the Activity class.