IllegalStateexception:onClick事件发生时无法执行活动的方法

时间:2014-04-13 06:31:04

标签: android

我对android开发很新。我开始创建简单的登录页面。当我试图验证输入的用户名时,我得到致命异常:主要。 我附上了我的代码供你审核。请建议我解决方案。

package com.msvd.form;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class LoginActivity extends ActionBarActivity {

public EditText et_username;
public EditText et_password;
public Button bt_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    et_username = (EditText) findViewById(R.id.editUsername);
    et_password = (EditText) findViewById(R.id.editPassword);
    bt_login = (Button) findViewById(R.id.buttonLogin);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}
 public void onClick(View view){
    String username = null;
    username = et_username.getText().toString();
    if(username.equals("demo"));{
    Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_LONG).show();
    startActivity(new Intent(this, MainActivity.class));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
        return rootView;
    }
}

}

我的xml文件。     

<TextView
    android:id="@+id/textpassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textusername"
    android:layout_below="@+id/editUsername"
    android:layout_marginTop="21dp"
    android:text="Password"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textpassword"
    android:layout_alignBottom="@+id/textpassword"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/textpassword"
    android:ems="10"
    android:text="********" />

<EditText
    android:id="@+id/editUsername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editPassword"
    android:layout_alignTop="@+id/textusername"
    android:ems="10"
    android:text="username" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textusername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="351dp"
    android:layout_marginTop="153dp"
    android:text="Username"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/buttonLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editPassword"
    android:layout_marginTop="23dp"
    android:layout_toRightOf="@+id/textpassword"
    android:text="Login" 
    android:onClick="onClick"/>

 </RelativeLayout>

清单文件

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.msvd.form.LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.msvd.form.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter >
            <action android:name = "com.msvd.form.MainActivity"/>
            <category android:name = "android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

</application>

</manifest>

我的错误日志

04-13 11:16:57.824: I/dalvikvm(567): threadid=3: reacting to signal 3
04-13 11:16:58.184: I/dalvikvm(567): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:16:58.323: I/dalvikvm(567): threadid=3: reacting to signal 3
04-13 11:16:58.424: I/dalvikvm(567): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:16:58.764: D/gralloc_goldfish(567): Emulator without GPU emulation detected.
04-13 11:16:58.823: I/dalvikvm(567): threadid=3: reacting to signal 3
04-13 11:16:58.873: I/dalvikvm(567): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:15.313: I/dalvikvm(615): threadid=3: reacting to signal 3
04-13 11:25:15.363: I/dalvikvm(615): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:15.753: I/dalvikvm(615): threadid=3: reacting to signal 3
04-13 11:25:15.803: I/dalvikvm(615): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:15.933: D/gralloc_goldfish(615): Emulator without GPU emulation detected.
04-13 11:25:16.124: I/dalvikvm(615): threadid=3: reacting to signal 3
04-13 11:25:16.203: I/dalvikvm(615): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:23.813: D/AndroidRuntime(615): Shutting down VM
04-13 11:25:23.813: W/dalvikvm(615): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-13 11:25:23.873: E/AndroidRuntime(615): FATAL EXCEPTION: main
04-13 11:25:23.873: E/AndroidRuntime(615): java.lang.IllegalStateException: Could not execute method of the activity
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.view.View$1.onClick(View.java:3044)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.view.View.performClick(View.java:3511)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.view.View$PerformClick.run(View.java:14105)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.os.Handler.handleCallback(Handler.java:605)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.os.Looper.loop(Looper.java:137)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 11:25:23.873: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:25:23.873: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:25:23.873: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 11:25:23.873: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 11:25:23.873: E/AndroidRuntime(615):  at dalvik.system.NativeStart.main(Native Method)
04-13 11:25:23.873: E/AndroidRuntime(615): Caused by: java.lang.reflect.InvocationTargetException
04-13 11:25:23.873: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:25:23.873: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:25:23.873: E/AndroidRuntime(615):  at android.view.View$1.onClick(View.java:3039)
04-13 11:25:23.873: E/AndroidRuntime(615):  ... 11 more
04-13 11:25:23.873: E/AndroidRuntime(615): Caused by: java.lang.NullPointerException
04-13 11:25:23.873: E/AndroidRuntime(615):  at com.msvd.form.LoginActivity.onClick(LoginActivity.java:41)
04-13 11:25:23.873: E/AndroidRuntime(615):  ... 14 more
04-13 11:25:24.713: I/dalvikvm(615): threadid=3: reacting to signal 3
04-13 11:25:24.744: I/dalvikvm(615): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:26.723: I/Process(615): Sending signal. PID: 615 SIG: 9
04-13 11:25:37.403: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:37.623: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:38.095: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:38.143: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:38.613: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:38.663: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:39.093: D/gralloc_goldfish(635): Emulator without GPU emulation detected.
04-13 11:25:39.103: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:39.153: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:39.733: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:39.783: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:45.163: D/AndroidRuntime(635): Shutting down VM
04-13 11:25:45.163: W/dalvikvm(635): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-13 11:25:45.213: E/AndroidRuntime(635): FATAL EXCEPTION: main
04-13 11:25:45.213: E/AndroidRuntime(635): java.lang.IllegalStateException: Could not execute method of the activity
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.view.View$1.onClick(View.java:3044)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.view.View.performClick(View.java:3511)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.view.View$PerformClick.run(View.java:14105)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.os.Handler.handleCallback(Handler.java:605)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.os.Looper.loop(Looper.java:137)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 11:25:45.213: E/AndroidRuntime(635):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:25:45.213: E/AndroidRuntime(635):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:25:45.213: E/AndroidRuntime(635):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 11:25:45.213: E/AndroidRuntime(635):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 11:25:45.213: E/AndroidRuntime(635):  at dalvik.system.NativeStart.main(Native Method)
04-13 11:25:45.213: E/AndroidRuntime(635): Caused by: java.lang.reflect.InvocationTargetException
04-13 11:25:45.213: E/AndroidRuntime(635):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:25:45.213: E/AndroidRuntime(635):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:25:45.213: E/AndroidRuntime(635):  at android.view.View$1.onClick(View.java:3039)
04-13 11:25:45.213: E/AndroidRuntime(635):  ... 11 more
04-13 11:25:45.213: E/AndroidRuntime(635): Caused by: java.lang.NullPointerException
04-13 11:25:45.213: E/AndroidRuntime(635):  at com.msvd.form.LoginActivity.onClick(LoginActivity.java:41)
04-13 11:25:45.213: E/AndroidRuntime(635):  ... 14 more
04-13 11:25:45.603: D/dalvikvm(635): GC_CONCURRENT freed 186K, 4% free 6728K/6983K, paused 7ms+28ms
04-13 11:25:45.963: I/dalvikvm(635): threadid=3: reacting to signal 3
04-13 11:25:45.993: I/dalvikvm(635): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:25:47.783: I/Process(635): Sending signal. PID: 635 SIG: 9
04-13 11:29:48.076: I/dalvikvm(681): threadid=3: reacting to signal 3
04-13 11:29:48.224: I/dalvikvm(681): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:29:48.523: I/dalvikvm(681): threadid=3: reacting to signal 3
04-13 11:29:48.573: I/dalvikvm(681): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:29:48.934: I/dalvikvm(681): threadid=3: reacting to signal 3
04-13 11:29:48.963: I/dalvikvm(681): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:29:49.433: I/dalvikvm(681): threadid=3: reacting to signal 3
04-13 11:29:49.463: I/dalvikvm(681): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:29:49.513: D/gralloc_goldfish(681): Emulator without GPU emulation detected.
04-13 11:29:53.563: D/AndroidRuntime(681): Shutting down VM
04-13 11:29:53.563: W/dalvikvm(681): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-13 11:29:53.673: E/AndroidRuntime(681): FATAL EXCEPTION: main
04-13 11:29:53.673: E/AndroidRuntime(681): java.lang.IllegalStateException: Could not execute method of the activity
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.view.View$1.onClick(View.java:3044)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.view.View.performClick(View.java:3511)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.view.View$PerformClick.run(View.java:14105)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.os.Handler.handleCallback(Handler.java:605)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.os.Looper.loop(Looper.java:137)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 11:29:53.673: E/AndroidRuntime(681):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:29:53.673: E/AndroidRuntime(681):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:29:53.673: E/AndroidRuntime(681):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 11:29:53.673: E/AndroidRuntime(681):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 11:29:53.673: E/AndroidRuntime(681):  at dalvik.system.NativeStart.main(Native Method)
04-13 11:29:53.673: E/AndroidRuntime(681): Caused by: java.lang.reflect.InvocationTargetException
04-13 11:29:53.673: E/AndroidRuntime(681):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:29:53.673: E/AndroidRuntime(681):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:29:53.673: E/AndroidRuntime(681):  at android.view.View$1.onClick(View.java:3039)
04-13 11:29:53.673: E/AndroidRuntime(681):  ... 11 more
04-13 11:29:53.673: E/AndroidRuntime(681): Caused by: java.lang.NullPointerException
04-13 11:29:53.673: E/AndroidRuntime(681):  at com.msvd.form.LoginActivity.onClick(LoginActivity.java:42)
04-13 11:29:53.673: E/AndroidRuntime(681):  ... 14 more
04-13 11:29:54.523: I/dalvikvm(681): threadid=3: reacting to signal 3
04-13 11:29:54.647: I/dalvikvm(681): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:29:57.274: I/Process(681): Sending signal. PID: 681 SIG: 9
04-13 11:43:26.193: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:43:26.333: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:43:26.663: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:43:26.733: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:43:27.084: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:43:28.094: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:43:28.214: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:43:28.244: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:43:28.833: D/gralloc_goldfish(729): Emulator without GPU emulation detected.
04-13 11:43:29.194: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:43:29.223: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:44:37.027: D/AndroidRuntime(729): Shutting down VM
04-13 11:44:37.027: W/dalvikvm(729): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-13 11:44:37.144: E/AndroidRuntime(729): FATAL EXCEPTION: main
04-13 11:44:37.144: E/AndroidRuntime(729): java.lang.IllegalStateException: Could not execute method of the activity
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.view.View$1.onClick(View.java:3044)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.view.View.performClick(View.java:3511)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.view.View$PerformClick.run(View.java:14105)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.os.Handler.handleCallback(Handler.java:605)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.os.Looper.loop(Looper.java:137)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 11:44:37.144: E/AndroidRuntime(729):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:44:37.144: E/AndroidRuntime(729):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:44:37.144: E/AndroidRuntime(729):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 11:44:37.144: E/AndroidRuntime(729):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 11:44:37.144: E/AndroidRuntime(729):  at dalvik.system.NativeStart.main(Native Method)
04-13 11:44:37.144: E/AndroidRuntime(729): Caused by: java.lang.reflect.InvocationTargetException
04-13 11:44:37.144: E/AndroidRuntime(729):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 11:44:37.144: E/AndroidRuntime(729):  at java.lang.reflect.Method.invoke(Method.java:511)
04-13 11:44:37.144: E/AndroidRuntime(729):  at android.view.View$1.onClick(View.java:3039)
04-13 11:44:37.144: E/AndroidRuntime(729):  ... 11 more
04-13 11:44:37.144: E/AndroidRuntime(729): Caused by: java.lang.NullPointerException
04-13 11:44:37.144: E/AndroidRuntime(729):  at com.msvd.form.LoginActivity.onClick(LoginActivity.java:42)
04-13 11:44:37.144: E/AndroidRuntime(729):  ... 14 more
04-13 11:44:37.854: I/dalvikvm(729): threadid=3: reacting to signal 3
04-13 11:44:37.994: I/dalvikvm(729): Wrote stack traces to '/data/anr/traces.txt'
04-13 11:44:40.235: I/Process(729): Sending signal. PID: 729 SIG: 9
请告诉我该怎么办?感谢

3 个答案:

答案 0 :(得分:0)

et_username.getText()为空。这就是你的应用崩溃的原因,因为你在空对象上调用了toString()

对此的简单保护是Yoda statement

if( "demo".equals( username ) ) {
}

此信息由您的堆栈跟踪提供,查看异常的最主要原因,直到您到达代码行,此处为活动的第41行。

答案 1 :(得分:0)

试试这个..

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    public EditText et_username;
    public EditText et_password;
    public Button bt_login;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
        et_username = (EditText) rootView.findViewById(R.id.editUsername);
        et_password = (EditText) rootView.findViewById(R.id.editPassword);
        bt_login = (Button) rootView.findViewById(R.id.buttonLogin);

        bt_login.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String username = null;
            username = et_username.getText().toString();
            if(username.equals("demo"));{
                    Toast.makeText(getActivity(), "Login Successful", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(getActivity(), MainActivity.class));
             }
        }

        });
        return rootView;
    }
}

修改

<Button
    android:id="@+id/buttonLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editPassword"
    android:layout_marginTop="23dp"
    android:layout_toRightOf="@+id/textpassword"
    android:text="Login" />

答案 2 :(得分:-1)

在if语句后删除半冒号

试试这个

 public void onClick(View view){
String username = et_username.getText().toString();
if(username.equals("demo")){
Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, MainActivity.class));
}
}