2013-06-30 161 views
0

當我點擊我的應用我的發送按鈕的應用程序崩潰與:當我點擊按鈕我的應用程序崩潰

06-30 13:57:46.487: E/AndroidRuntime(12201): FATAL EXCEPTION: main 
    06-30 13:57:46.487: E/AndroidRuntime(12201): java.lang.IllegalStateException: 
Could not find a method send(View) in the activity class com.yoursite.helloworld.MainActivity 
for onClick handler on view class android.widget.Button with id 'sendButton' 

我MainActivity.java:

 package com.yoursite.helloworld; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 

    public class MainActivity extends Activity { 

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


     @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; 
     } 

    } 

my HelloWorldActivity.java: 
package com.yoursite.helloworld; 

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.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class HelloWorldActivity extends Activity { 
    Button sendButton; 

    EditText msgTextField; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     // load the layout 
     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); 

    } 

    // this is the function that gets called when you click the button 
    public void send(View v) 
    { 
     // 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://mysite.com/test.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("user", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("name", 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(); 
     } 

    } 
} 

我activity_main.xml中:

<?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" 
    > 
    <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" 
     /> 

</LinearLayout> 

和AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.yoursite.helloworld" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 
<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.yoursite.helloworld.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> 

我試圖按照這個教程http://webtutsdepot.com/2011/11/15/android-tutorial-how-to-post-data-from-an-android-app-to-a-website/並嘗試一切都沒有成功,需要一些初學者的幫助,請!

回答

2

您的send()方法在HelloWorldActivity中,而您的佈局在MainActivity中被誇大。

將您的send()方法移動到MainActivity,或者在您的其他活動中膨脹佈局。

+0

夠公平的,評論刪除 –

+0

@Raghav好,所以我把send()移動到主要活動,但是當我放入消息並按下按鈕時,我得到java.lang.IllegalStateException:無法執行活動的方法 – BluGeni

相關問題