致命异常:主进程java.lang.IllegalStateException:无法执行android的方法:onClick

时间:2016-04-12 06:17:58

标签: android logging

我正在执行简单的php注册和登录页面,同时运行我正在获取的程序:

FATAL EXCEPTION : main process java.lang.IllegalStateException: Could not execute method for android:onClick

请帮我解决这个问题

MainActivity.java

 package com.dropouts.phplogin;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity{

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

    public void userLogin(View view)
    {

    }
    public void userReg(View view)
    {
        startActivity(new Intent(this,Register.class));


    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dropouts.phplogin.MainActivity"
    android:background="#651316"
    android:clickable="false">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="user_name"
        android:layout_alignParentTop="true"
        android:layout_marginTop="63dp"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignStart="@+id/editText2"
        android:layout_alignRight="@+id/editText2"
        android:layout_alignEnd="@+id/editText2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="password"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="login"
        android:id="@+id/lgn"
        android:layout_below="@+id/editText2"
        android:layout_alignLeft="@+id/editText2"
        android:layout_alignStart="@+id/editText2"
        android:layout_marginTop="73dp"
        android:onClick="userLogin"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="register"
        android:id="@+id/reg"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/lgn"
        android:layout_alignLeft="@+id/lgn"
        android:layout_alignStart="@+id/lgn"
        android:onClick="userReg"/>

</RelativeLayout>

    **Register.java**

package com.dropouts.phplogin;

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

/**
 * Created by Pankaj on 4/11/2016.
 */
public class Register extends Activity {

    EditText ed_name,ed_user_name,ed_user_pass;
    String name,user_name,user_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        ed_name = (EditText) findViewById(R.id.ed_n);
        ed_user_name= (EditText) findViewById(R.id.ed_usr_nm);
        ed_user_pass=(EditText)findViewById(R.id.ed_pass);

    }

    public void userReg(View view)
    {

        name=ed_name.getText().toString();
        user_name=ed_user_name.getText().toString();
        user_pass=ed_user_pass.getText().toString();
       String method="registration";
        BackgroundTask backgroundTask=new BackgroundTask(this);
        backgroundTask.execute(method,name,user_name,user_pass);
        finish();
    }

}

register_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF4F00">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ed_n"
        android:hint="name"
        android:layout_gravity="center_horizontal"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ed_usr_nm"
        android:hint="user_name"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ed_pass"
        android:hint="password"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:onClick="userReg"/>
</LinearLayout>

BackgroundTask.java

package com.dropouts.phplogin;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import java.io.BufferedReader;
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.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * Created by Pankaj on 4/11/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://127.0.0.1/ppp/registration.php";
        String login_url = "http://127.0.0.1/ppp/login.php";

        String method = params[0];

        if (method.equals("registration")) {

            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];

            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("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                Os.close();
                InputStream is = httpURLConnection.getErrorStream();
                is.close();
                return "Registration success....";


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } 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();
        }
    }

** Log cat **

 E/AndroidRuntime: FATAL EXCEPTION: main


    Process: com.dropouts.phplogin, PID: 3229
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                    Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.dropouts.phplogin/com.dropouts.phplogin.Register}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
    at android.app.Activity.startActivityForResult(Activity.java:3917)
    at android.app.Activity.startActivityForResult(Activity.java:3877)
    at android.app.Activity.startActivity(Activity.java:4200)
    at android.app.Activity.startActivity(Activity.java:4168)
    at com.dropouts.phplogin.MainActivity.userReg(MainActivity.java:23)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

问题是您的Register活动未在清单中声明。仔细阅读最后一条异常消息:

引起:android.content.ActivityNotFoundException:无法找到显式活动类{com.dropouts.phplogin / com.dropouts.phplogin.Register};你有没有在AndroidManifest.xml中声明这个活动?