【问题标题】:Aplication unfortunately close but no code error - Java android [duplicate]应用程序不幸关闭但没有代码错误 - Java android [重复]
【发布时间】:2015-07-07 05:43:50
【问题描述】:

大家好, 我是 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();

        }
    });
}

登录.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。然后我调试我的应用程序并且没有错误。但是为什么当我按下主菜单上的登录按钮(进入 Login.java)时,它说“不幸的是登录已停止”?

【问题讨论】:

  • 请试试下面 Stefan Beike 给出的答案。如果仍然遇到错误,请复制粘贴 logcat。

标签: java android


【解决方案1】:

这行不通:

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);

【讨论】:

  • 我试过这个,它可以工作,但出现了一个问题。当我在 Login.java(Button log=(Button)findViewById(R.id.login);) 中按下按钮登录时,它再次强制关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 2013-08-03
相关资源
最近更新 更多