我正在尝试创建一个可以登录/注册用户的简单应用程序。所有的数据都是 存储在Mysql数据库中。 我的应用程序安装成功,但是当我打开它时,它给出了一个错误说“不幸的是,'myapp'已停止工作”。 我已经阅读了这里发布的几乎所有相关问题,但没有运气!
这是来自logcat。
07-25 01:22:31.452: E/AndroidRuntime(2101): FATAL EXCEPTION: main
07-25 01:22:31.452: E/AndroidRuntime(2101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqltest/com.example.mobileapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.os.Looper.loop(Looper.java:137)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 01:22:31.452: E/AndroidRuntime(2101): at dalvik.system.NativeStart.main(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.setContentView(Activity.java:1895)
07-25 01:22:31.452: E/AndroidRuntime(2101): at com.example.mobileapp.MainActivity.onCreate(MainActivity.java:45)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Activity.performCreate(Activity.java:5133)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 11 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.constructNative(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
07-25 01:22:31.452: E/AndroidRuntime(2101): ... 23 more
07-25 01:22:31.452: E/AndroidRuntime(2101): Caused by: java.lang.OutOfMemoryError
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.Resources.loadDrawable(Resources.java:2105)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3364)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.View.<init>(View.java:3293)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.view.ViewGroup.<init>(ViewGroup.java:453)
07-25 01:22:31.452: E/AndroidRuntime(2101): at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background" >
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:text="Password" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="38dp"
android:gravity="center"
android:text="PhilTaxWindow User Login"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/username"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="Username" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/register"
android:layout_alignBottom="@+id/register"
android:layout_marginRight="17dp"
android:layout_toLeftOf="@+id/register"
android:text="Don't Have an Account?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/register"
android:layout_alignParentLeft="true"
android:layout_marginBottom="60dp"
android:text="Login" />
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tv2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Button01"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:ems="10"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv"
android:layout_alignParentRight="true"
android:layout_marginBottom="17dp"
android:layout_marginRight="29dp"
android:text="Register here" />
</RelativeLayout>
mainactivity.java
package com.example.mobileapp;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.mysqltest.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText Username, Password;
private Button Submit, Register;
private ProgressDialog Dialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.4/checkingPHP/CheckUser.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.username);
Password = (EditText)findViewById(R.id.password);
Submit = (Button)findViewById(R.id.Button01);
Register = (Button)findViewById(R.id.register);
Submit.setOnClickListener(this);
Register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
Dialog = new ProgressDialog(MainActivity.this);
Dialog.setMessage("Validating...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
String username = Username.getText().toString();
String password = Password.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, WelcomPage.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
if (file_url != null){
Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:0)
模拟器遇到内存不足异常
由于, Caused by: java.lang.OutOfMemoryError
android:background="@drawable/background"
您尝试加载的图片导致此问题。尝试降低图像分辨率。