应用程序意外停止请稍后再试 - 强制关闭

时间:2014-04-04 05:46:24

标签: java android

现在只是尝试创建一个使用eclipse登录的Android应用程序。 但当我运行代码得到上面提到的错误。看着logcat它显示第36行的错误,但我不知道如何恢复它,

这是我的代码:

package com.example.login;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import android.app.Activity;

public class MainActivity extends ActionBarActivity {
    EditText un,pw;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

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

                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));

                String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("<target page url>", postParameters);
                    String res=response.toString();
                    res= res.replaceAll("\\s+","");
                    if(res.equals("1"))
                        error.setText("Correct Username or Password");
                    else
                        error.setText("Sorry!! Incorrect Username or Password");
                } catch (Exception e) {
                    un.setText(e.toString());
                }

            }
        });
    }
}

这是我的logcat:

04-04 11:14:26.657: E/AndroidRuntime(382): FATAL EXCEPTION: main
04-04 11:14:26.657: E/AndroidRuntime(382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.MainActivity}: java.lang.NullPointerException
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.os.Looper.loop(Looper.java:132)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.main(ActivityThread.java:4123)
04-04 11:14:26.657: E/AndroidRuntime(382):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 11:14:26.657: E/AndroidRuntime(382):  at java.lang.reflect.Method.invoke(Method.java:491)
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-04 11:14:26.657: E/AndroidRuntime(382):  at dalvik.system.NativeStart.main(Native Method)
04-04 11:14:26.657: E/AndroidRuntime(382): Caused by: java.lang.NullPointerException
04-04 11:14:26.657: E/AndroidRuntime(382):  at com.example.login.MainActivity.onCreate(MainActivity.java:36)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.Activity.performCreate(Activity.java:4397)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
04-04 11:14:26.657: E/AndroidRuntime(382):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
04-04 11:14:26.657: E/AndroidRuntime(382):  ... 11 more

如果我能解决这个问题,那就太明显了。 提前致谢。

3 个答案:

答案 0 :(得分:1)

在创建

中添加以下代码行
 un=(EditText)findViewById(R.id.et_un);

希望这能解决问题

答案 1 :(得分:1)

声明这一点,您忘记在onCreate方法中初始化用户名的编辑文本。你已经声明了pw,Ok和错误但是忘了宣布un。所以声明如下,然后尝试。

un=(EditText)findViewById(R.id.yourID);

答案 2 :(得分:1)

ok=(Button)findViewById(R.id.btn_login);
//...
ok.setOnClickListener(...)

确保您的activity_main布局确实包含ID为btn_login的视图。 ok在这里为空是对堆栈跟踪中NPE的唯一解释。

un也没有像其他人所提到的那样进行初始化,但这只会导致后来的NPE,其中不同的堆栈跟踪涉及onClick()而不是onCreate()