不幸的是,当onclick事件在android

时间:2015-05-07 17:13:44

标签: android

我有一个应用程序,它通过Android中的EditText获取输入,并通过php服务将这些值发送回数据库。 php服务是正确的。但它发送给定的错误。为什么..?

05-07 22:36:25.882: D/ActivityThread(21174): handleBindApplication:com.example.pgfinder
05-07 22:36:25.932: W/ApplicationPackageManager(21174): getCSCPackageItemText()
05-07 22:36:25.932: D/DisplayManager(21174): DisplayManager()
05-07 22:36:26.233: D/OpenGLRenderer(21174): Enabling debug mode 0
05-07 22:36:26.303: I/Timeline(21174): Timeline: Activity_idle id: android.os.BinderProxy@41e5bc30 time:53798431
05-07 22:36:27.784: I/Timeline(21174): Timeline: Activity_launch_request id:com.example.pgfinder time:53799926
05-07 22:36:27.814: W/ApplicationPackageManager(21174): getCSCPackageItemText()
05-07 22:36:28.094: I/Timeline(21174): Timeline: Activity_idle id: android.os.BinderProxy@41ebc438 time:53800237
05-07 22:36:28.705: D/AndroidRuntime(21174): Shutting down VM
05-07 22:36:28.705: W/dalvikvm(21174): threadid=1: thread exiting with uncaught exception (group=0x41689bc0)
05-07 22:36:28.715: E/AndroidRuntime(21174): FATAL EXCEPTION: main
05-07 22:36:28.715: E/AndroidRuntime(21174): Process: com.example.pgfinder, PID: 21174
05-07 22:36:28.715: E/AndroidRuntime(21174): java.lang.NullPointerException
05-07 22:36:28.715: E/AndroidRuntime(21174):    at com.example.pgfinder.Signup$2.onClick(Signup.java:64)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.view.View.performClick(View.java:4496)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.view.View$PerformClick.run(View.java:18603)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.os.Handler.handleCallback(Handler.java:733)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.os.Looper.loop(Looper.java:136)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at android.app.ActivityThread.main(ActivityThread.java:5433)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at java.lang.reflect.Method.invoke(Method.java:515)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
05-07 22:36:28.715: E/AndroidRuntime(21174):    at dalvik.system.NativeStart.main(Native Method)
05-07 22:36:29.916: I/Process(21174): Sending signal. PID: 21174 SIG: 9
05-07 22:36:30.066: D/ActivityThread(21305): handleBindApplication:com.example.pgfinder
05-07 22:36:30.096: W/ApplicationPackageManager(21305): getCSCPackageItemText()
05-07 22:36:30.096: D/DisplayManager(21305): DisplayManager()
05-07 22:36:30.247: D/OpenGLRenderer(21305): Enabling debug mode 0
05-07 22:36:30.287: I/Timeline(21305): Timeline: Activity_idle id: android.os.BinderProxy@41e570a8 time:53802425

,代码是..

package com.example.pgfinder;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.pgfinder.handler.ServiceHandler;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Signup extends ActionBarActivity 
{
EditText Name,Email,Phone,Password,Confirm_Password,City;
Button backBtn, signupBtn;
String name,email,phone,password,c_password,city,jsonFormat;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        backBtn=(Button) findViewById(R.id.back);
        signupBtn=(Button) findViewById(R.id.signup);
        backBtn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Intent in = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(in);
                Name=(EditText) findViewById(R.id.name);
                Email=(EditText) findViewById(R.id.email);
                Phone=(EditText) findViewById(R.id.phone);
                Password=(EditText) findViewById(R.id.password);
                Confirm_Password=(EditText) findViewById(R.id.confirm_password);
                City=(EditText) findViewById(R.id.city);
            }
        });
    signupBtn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub


                    name=Name.getText().toString();
                    email=Email.getText().toString();
                    phone=Phone.getText().toString();
                    password=Password.getText().toString();
                    c_password=Confirm_Password.getText().toString();
                    city=City.getText().toString();




                if (name.equals("") || email.equals("")) 
                {
                    Toast.makeText(getApplicationContext(),
                            "Enter FirstName & Email", 5000).show();

                } else {
                    new SignupTask()
                            .execute("www.webpage.com/usersignup.php");

                }




            }
        });
    }

    class SignupTask extends AsyncTask<String, Void, String> 
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            Log.i("TAG", "URL : " + params[0]);
            Log.i("TAG", "registerStr : " + name);

            ServiceHandler sh = new ServiceHandler();

            List<NameValuePair> nameValuePairsList = new ArrayList<NameValuePair>();
            nameValuePairsList.add(new BasicNameValuePair("name", name));
            nameValuePairsList.add(new BasicNameValuePair("email", email));
            nameValuePairsList.add(new BasicNameValuePair("phone", phone));
            nameValuePairsList.add(new BasicNameValuePair("city", city));
            nameValuePairsList.add(new BasicNameValuePair("password", password));
            nameValuePairsList.add(new BasicNameValuePair("c_password", c_password));
            String res = sh.makeServiceCall(params[0], ServiceHandler.GET,
                    nameValuePairsList);

            Log.i("TAG", "RESPONSE : " + res);
            return res;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            JSONObject jObject;
            try 
            {
                jObject = new JSONObject(result);

                //String response = jObject.getString("success").toString();

                Toast.makeText(getApplicationContext(), result, 3000).show();
                if (jObject.getString("success").toString()
                        .equalsIgnoreCase("1")) {
                    Toast.makeText(getApplicationContext(),
                            "done", 3000).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "fuck off", 3000).show();
                }
            } 
            catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误。这些是:

  

05-07 22:36:28.715:E / AndroidRuntime(21174):at   com.example.pgfinder.Signup $ 2.onClick(Signup.java:64)

通过查看您的logcat,您的第一个问题就在这里

signupBtn.setOnClickListener(new OnClickListener() 
     {
         @Override
         public void onClick(View view) {
                // TODO Auto-generated method stub

            }
        });

您需要发布XML布局,以便我们可以确切地告诉您错误的位置。您可能在XML中使用了不同的按钮ID,并且您正在尝试访问其他一些ID。但不确定。您需要先发布您的布局。

此外,你应该移动这一切

Name=(EditText) findViewById(R.id.name);
Email=(EditText) findViewById(R.id.email);
Phone=(EditText) findViewById(R.id.phone);
Password=(EditText) findViewById(R.id.password);
Confirm_Password= (EditText)findViewById(R.id.confirm_password);
City=(EditText) findViewById(R.id.city);

之前的setOnClickListener

Name=(EditText) findViewById(R.id.name);
Email=(EditText) findViewById(R.id.email);
Phone=(EditText) findViewById(R.id.phone);
Password=(EditText) findViewById(R.id.password);
Confirm_Password=(EditText)findViewById (R.id.confirm_password);
City=(EditText) findViewById(R.id.city);

backBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                }
            });

另外,请使用Toast方法

 Toast.makeText(MainActivity.class,"Enter FirstName & Email",Toast.LENGTH_LONG).show();

根据您的需要更改长度或长度。

我不知道为什么后面的按钮onClickListener中有intent。为什么呢?

 Intent in = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(in);