这个问题在这里已经有答案了:
不幸的是,myapp已经停止。我怎样才能解决这个问题(20个答案)
5年前关门了。
大家好,我是新来的安卓编程,我试图让登录应用程序连接到本地主机mysql与安卓工作室基于这个网站。代码如下:
主活动.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());
}
}
一切都很好,我可以在添加之前访问user.java private void masuk
. 然后我调试我的应用程序,没有错误。但为什么当我按下主菜单上的登录按钮(进入login.java)时,它会说“不幸的是登录已经停止了”?
1条答案
按热度按时间41zrol4v1#
这不起作用:
首先打电话
setContentView
. 然后分配gui元素:或者如果你需要他们作为班级成员: