Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我点击按钮时,我的应用程序正在崩溃_Java_Android - Fatal编程技术网

Java 当我点击按钮时,我的应用程序正在崩溃

Java 当我点击按钮时,我的应用程序正在崩溃,java,android,Java,Android,当我单击应用程序上的“发送”按钮时,应用程序会崩溃,原因是: 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

当我单击应用程序上的“发送”按钮时,应用程序会崩溃,原因是:

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'
my 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();
        }

    }
}
package com.yoursite.helloworld;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
my HelloWorldActivity.java:
包com.yoursite.helloworld;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类HelloWorldActivity扩展了活动{
按钮发送按钮;
编辑文本msgTextField;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.activity_main);
//使消息文本字段成为对象
msgTextField=(EditText)findViewById(R.id.msgTextField);
//生成发送按钮对象
sendButton=(按钮)findViewById(R.id.sendButton);
}
//这是单击按钮时调用的函数
公共无效发送(视图五)
{
//从消息文本框中获取消息
字符串msg=msgTextField.getText().toString();
//确保字段不是空的
如果(msg.length()>0)
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://mysite.com/test.php");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户”、“12345”);
添加(新的BasicNameValuePair(“name”,msg));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(“”;//清除文本框
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
其他的
{
//如果文本字段为空,则显示消息
Toast.makeText(getBaseContext(),“所有字段都是必需的”,Toast.LENGTH_SHORT.show();
}
}
}
my 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>

并且尝试了所有的方法都没有成功,需要一些初学者的帮助吗

send()方法在
HelloWorldActivity
中,而布局在
MainActivity
中膨胀


将send()方法移动到
MainActivity
,或者在其他活动中扩大布局。

很公平,请发表评论removed@Raghav好的,我将send()移到了主活动中,但当我输入消息并按下按钮时,我得到java.lang.IllegalStateException:无法执行活动的方法