不幸的是,应用程序关闭但没有代码错误 - Java android

时间:2015-04-28 11:33:47

标签: java android

Helo伙计们, 我是android编程的新手,我正在尝试使用基于this web的android studio连接到localhost mysql的登录应用程序。这是代码:

Mainactivity.java

public class Mainmenu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainmenu);

    Button login=(Button)findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent login=new Intent(v.getContext(),Login.class);
            startActivity(login);
        }
    });

    Button register=(Button)findViewById(R.id.register);
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent  regis= new Intent(v.getContext(),Register.class);
            startActivity(regis);
        }
    });

    Button exit=(Button)findViewById(R.id.exit);
    exit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();

        }
    });
}

Login.java

public class Login extends ActionBarActivity {
        final EditText id=(EditText)findViewById(R.id.handphone);
        final EditText pass=(EditText)findViewById(R.id.pass_login);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

        TextView login=(TextView)findViewById(R.id.textView);
        login.setText("Login to Human Tracker");

        Button log=(Button)findViewById(R.id.login);
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=pass.getText().toString();
                String handphone=id.getText().toString();

                if (!password.equals("") && !handphone.equals("")) {
                    Toast.makeText(getApplication(),"Your id or password is wrong",Toast.LENGTH_SHORT).show();

                } else {
                    masuk();
                    Toast.makeText(getApplication(),"Welcome", Toast.LENGTH_SHORT).show();
                    Intent user = new Intent(v.getContext(), User.class);
                    startActivity(user);

                }
            }
        });
    }

    private void masuk(){

    SharedPreferences prefs;
    String prefName ="report";

    InputStream is=null;
    String result=null;
    String line=null;
    JSONObject jArray= null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("hp",id.getText().toString()));

    try {
        HttpClient httpclient=new DefaultHttpClient();
        HttpPost httppost=new HttpPost("http://10.0.0.2");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity=response.getEntity();
        is=entity.getContent();
    } catch (Exception e){
        Log.e("Fail 1: Error in HTTP connection",e.toString());
        Toast.makeText(getApplicationContext(),"Fail 1: Error in HTTP connection",Toast.LENGTH_SHORT).show();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("Fail 2: Error converting result ", e.toString());
    }

    try
    {
        JSONObject jobject = new JSONObject(result);

        String S_pwd = jobject.getString("pass");
        String S_name = jobject.getString("name");
        String S_id = jobject.getString("id");

        if(S_pwd.equals(pass.getText().toString())) {

            Toast.makeText(getBaseContext(), "Login Successfully",
                    Toast.LENGTH_SHORT).show();

            prefs = getSharedPreferences(prefName, MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();

            //---save the values in the EditText view to preferences---
            editor.putString("id", S_id);
            editor.putString("name", S_name);

            //---saves the values---
            editor.commit();

            Toast.makeText(getApplicationContext(),"Login success",Toast.LENGTH_SHORT).show();
            }

        else {
            Toast.makeText(getBaseContext(), "Login Failure \n" +
                    "\n Try Again", Toast.LENGTH_LONG).show();

            id.setText("");
            pass.setText("");
        }
    }
    catch(Exception e)
    {
        Log.e("Fail 3", e.toString());
    }
}

一切都很好,我可以在添加private void masuk之前转到User.java。然后我调试我的应用程序,没有错误。但是为什么当我按下主菜单上的登录按钮(去登录.java)它说“遗憾的是登录已停止”?

1 个答案:

答案 0 :(得分:4)

这不起作用:

public class Login extends ActionBarActivity {
        final EditText id=(EditText)findViewById(R.id.handphone);
        final EditText pass=(EditText)findViewById(R.id.pass_login);

首先致电setContentView。 之后分配Gui元素:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText id=(EditText)findViewById(R.id.handphone);
        EditText pass=(EditText)findViewById(R.id.pass_login);

或者如果你需要他们作为班级成员:

 EditText id;
 EditText pass;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        id=(EditText)findViewById(R.id.handphone);
        pass=(EditText)findViewById(R.id.pass_login);