我正在运行我的Android应用程序,当我使用图像@ drawable / image(图像大小为48kb)时,它会崩溃,但是当我使用@ mipmap / ic_launcher时,它会运行。我希望能够在开发时使用自己选择的图像。
我已经在这里和其他站点搜索了更简单的错误,但没有得到可靠的解决方法。
我该如何解决错误。
这是错误消息
03-15 09:09:08.821 20006-20006/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lecevaluation, PID: 20006
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lecevaluation/com.example.lecevaluation.MainActivity}: android.view.InflateException: Binary XML file line #35: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:771)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.lecevaluation.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060058 a=-1 r=0x7f060058}
at android.content.res.Resources.loadDrawable(Resources.java:3425)
at android.content.res.TypedArray.getDrawable(TypedArray.java:614)
at android.widget.ImageView.<init>(ImageView.java:134)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:771)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.lecevaluation.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
at dalvik.system.NativeStart.main(Native Method)
03-15 09:09:09.341 620-917/? E/Watchdog: !@Sync 106
这是我的MainActivity.java
package com.example.lecevaluation;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
private static final String URL_FOR_LOGIN = "https://XXX.XXX.X.XX/android_login/login.php";
/**
* the url is C:\wamp64\www\android_login\login.php
*/
ProgressDialog progressDialog;
private EditText loginInputregNo, loginInputPassword;
private Button btnlogin;
private Button btnLinkSignup;
TextView textView;
int counter = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginInputregNo = (EditText) findViewById(R.id.login_input_regNo);
loginInputPassword = (EditText) findViewById(R.id.login_input_password);
btnlogin = (Button) findViewById(R.id.btn_login);
btnLinkSignup = (Button) findViewById(R.id.btn_link_signup);
textView = (TextView)findViewById(R.id.textView3);
textView.setVisibility(View.GONE);
//Progress Dialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginUser(loginInputregNo.getText().toString(),
loginInputPassword.getText().toString());
}
});
btnLinkSignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
private void loginUser(final String reg_no, final String password){
//Tag used to cancel the request
String cancel_req_tag = "login";
progressDialog.setMessage("Logging you in ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
URL_FOR_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString()
);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error){
String User = jObj.getJSONObject("user").getString("reg_no");
/**
* Launch User Activity
* Reached Here
*/Intent intent = new Intent(
MainActivity.this,
UserActivity.class);
intent.putExtra("reg_no" //or regNo or username
, User);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
textView.setVisibility(View.VISIBLE);
textView.setBackgroundColor(Color.RED);
counter--;
textView.setText(Integer.toString(counter));
if (counter == 0) {
btnlogin.setEnabled(false);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("reg_no", reg_no);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
这是我的activity_main
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="20dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/Lec"
android:id = "@+id/textView1"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true"
android:textColor = "#ff7aff24"
android:textSize = "35sp" />
<TextView
android:text = "@string/Login"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/textView"
android:textSize = "35sp"
android:layout_marginTop="10dp"
android:layout_below = "@+id/textView1"
android:layout_centerHorizontal = "true" />
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:src="@drawable/kenya"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/login_input_regNo"
android:hint= "@string/hint_regNo"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "30dp"
android:layout_below = "@+id/imageView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/login_input_password"
android:layout_below="@+id/login_input_regNo"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/login_input_regNo"
android:layout_alignEnd="@+id/login_input_regNo"
android:textColorHint="#ffff299f"
android:hint="@string/hint_password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/attempts"
android:id="@+id/textView2"
android:layout_below="@+id/login_input_password"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:textSize="25sp"
android:layout_toRightOf="@+id/textView2"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"/>
<Button android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_login"
android:layout_marginTop="30dp"
android:layout_below="@+id/textView2"
android:textColor="@android:color/black"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"/>
<Button android:id="@+id/btn_link_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_sign_up"
android:textColor="@android:color/black"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2"
android:layout_below="@id/textView3"
android:layout_marginTop="30dp"
android:layout_marginStart="70dp"
android:layout_marginLeft="50dp"/>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:1)
如果您清楚地看到日志,则会发现异常:
原因:android.content.res.Resources $ NotFoundException:资源 不是Drawable(颜色或路径)
这意味着您提供的图像可能位于ex drawable-v24
的不同drawble文件夹中(仅适用于api> = 24,但不适用于api <24),这就是您的应用无法找到该图像的原因,因此您需要将该图像复制到可绘制文件夹中,为此,首先必须打开项目视图结构,然后执行以下操作:
Project-> app-> src-> main-> res->在这里,您将看到不同的drawable 文件夹
答案 1 :(得分:0)
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:src="@drawable/kenya"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
在此代码块中,请检查肯尼亚在可绘制的文件夹中。因为在stacktrace中,原因是:android.content.res.Resources $ NotFoundException:资源不是可绘制对象(颜色或路径):TypedValue {t = 0x1 / d = 0x7f060058 a = -1 r = 0x7f060058}
答案 2 :(得分:-1)
您需要将android:src
更改为app:srcCompat
。
还要看一下这个问题:Android: What is the difference between app:srcCompat=" " and android:src=" "?