我怎么能解决java.lang.IllegalStateException:无法执行活动的方法?

时间:2016-09-22 07:27:45

标签: java php

我正在使用登录和注册功能创建应用程序。我正在使用wamp服务器为我的应用程序执行数据库。但是当我在手机上运行应用程序时,它会一直显示以下错误;并且没有任何东西存储在本地主机数据库中。大约2周时间我一直在努力解决这个问题但仍然无法解决。有人可以帮助我吗?

16071/com.example.chang.e_ktm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chang.e_ktm, PID: 16071
java.lang.IllegalStateException: Could not execute method of the activity
 at android.view.View$1.onClick(View.java:4074)
 at android.view.View.performClick(View.java:4848)
 at android.view.View$PerformClick.run(View.java:20300)
 at android.os.Handler.handleCallback(Handler.java:815)
 at android.os.Handler.dispatchMessage(Handler.java:104)
 at android.os.Looper.loop(Looper.java:210)
 at android.app.ActivityThread.main(ActivityThread.java:5830)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
Caused by: java.lang.reflect.InvocationTargetException
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at android.view.View$1.onClick(View.java:4069)
 at android.view.View.performClick(View.java:4848) 
 at android.view.View$PerformClick.run(View.java:20300) 
 at android.os.Handler.handleCallback(Handler.java:815) 
 at android.os.Handler.dispatchMessage(Handler.java:104) 
 at android.os.Looper.loop(Looper.java:210) 
 at android.app.ActivityThread.main(ActivityThread.java:5830) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879) 
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 at android.view.ViewGroup.addViewInner(ViewGroup.java:4088)
 at android.view.ViewGroup.addView(ViewGroup.java:3938)
 at android.view.ViewGroup.addView(ViewGroup.java:3910)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:464)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:445)
 at android.app.Activity.setContentView(Activity.java:2218)
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at android.view.View$1.onClick(View.java:4069) 
 at android.view.View.performClick(View.java:4848) 
 at android.view.View$PerformClick.run(View.java:20300) 
 at android.os.Handler.handleCallback(Handler.java:815) 
 at android.os.Handler.dispatchMessage(Handler.java:104) 
 at android.os.Looper.loop(Looper.java:210) 
 at android.app.ActivityThread.main(ActivityThread.java:5830) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879) 

这是我的Register.java文件

   package com.example.chang.e_ktm;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.lang.String;


public class RegisterActivity extends Activity {
    Button reg_button;
    EditText etName, etUserName, etPassword, etAge, etEmail;
    String name, user_name, password, email;
    int age;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etName = (EditText) findViewById(R.id.etName);
        etUserName = (EditText) findViewById(R.id.etUserName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etAge = (EditText) findViewById(R.id.etAge);
        etEmail = (EditText) findViewById(R.id.etEmail);
        reg_button = (Button)findViewById(R.id.bRegister);


    }

    public void userReg(View view)
    {
        name = etName.getText().toString();
        user_name = etUserName.getText().toString();
        password = etPassword.getText().toString();
        age = Integer.parseInt(etAge.getText().toString());
        email = etEmail.getText().toString();
        String method = "register";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method, name,user_name,password,age,email);
        finish();
    }
}

,这个是BackgroundTask.java文件

 package com.example.chang.e_ktm;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by Chang on 21/9/2016.
 */
public class BackgroundTask extends AsyncTask<String,Void,String> {
    Context ctx;

    BackgroundTask(Context ctx)
    {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://192.168.0.104/register.php";
        String login_url = "http://192.168.0.104/login.php";
        String method = params[0];
        if(method.equals("register"))
        {
            String name = params[1];
            String user_name = params[2];
            String password = params[3];
            int age = Integer.parseInt(params[4]);
            String email = params[5];
            try{
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
                String data = URLEncoder.encode("user", "UTF-8") + "="+URLEncoder.encode(name,"UTF-8") +"&"+
                        URLEncoder.encode("user_name", "UTF-8") + "="+URLEncoder.encode(user_name, "UTF-8") +"&"+
                        URLEncoder.encode("password", "UTF-8") + "="+URLEncoder.encode(password, "UTF-8") +"&"+
                        URLEncoder.encode("age", "UTF-8") + "="+URLEncoder.encode(String.valueOf(age), "UTF-8") +"&"+
                        URLEncoder.encode("email", "UTF-8") + "="+URLEncoder.encode(email, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Registration Success";

            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Void...values){
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(String result){
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }


    public void execute(String method, String name, String username, String password, int age, String email) {
    }
}

0 个答案:

没有答案