我是Android编程的新手,并且遇到了很多麻烦。我终于让我的应用程序在API 23和16上的模拟器中工作我的目标是API 16.它在模拟器上运行没有问题,但是当我尝试在我的手机上使用它时(Google Nexus 5 API 23)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:orientation="vertical"
android:background="#F44336"
android:id="@+id/background"
tools:context="thereisstuffhere.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/factTextBox"
android:textSize="25dp"
android:textColor="#fff"/>
</LinearLayout>
package there is stuff here too;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView factBox;
LinearLayout bg;
Facts factHolder = new Facts(this);
Backgrounds backs = new Backgrounds();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factBox = (TextView) findViewById(R.id.factTextBox);
factBox.setText(factHolder.nextFact());
bg = (LinearLayout) findViewById(R.id.background);
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
bg.setOnTouchListener(new OnSwipeTouchListener(this)
{
public void onSwipeTop()
{
//Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight()
{
//Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
prev();
}
public void onSwipeLeft()
{
//Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
next();
}
public void onSwipeBottom()
{
//Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
private void next() {
factBox.setText(factHolder.nextFact());
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
}
private void prev() {
factBox.setText(factHolder.prevFact());
bg.setBackgroundColor(getResources().getColor(backs.getBackground()));
}
public void genFact(View view)
{
factBox.setText(factHolder.nextFact());
}
Facts.java只包含一个巨大的列表和两个函数,它们返回列表中的下一个或上一个字符串
Backgrounds.java基本上只做同样的事情,它只使用一个Integer列表和colors.xml文件来存储标签等
这是我的OnTouchListener类:
package stuff be here;
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx)
{
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
return false;
}
private final class GestureListener extends SimpleOnGestureListener{
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e)
{
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
boolean result = false;
try{
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if(Math.abs(diffX) > Math.abs(diffY)){
if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD){
if(diffX > 0){
onSwipeRight();
}else{
onSwipeLeft();
}
}
result = true;
}
else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD){
if(diffY > 0){
onSwipeBottom();
}else{
onSwipeTop();
}
}
result = true;
} catch (Exception exception){
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight(){}
public void onSwipeLeft(){}
public void onSwipeTop(){}
public void onSwipeBottom(){}
}
我为所有代码道歉,但我一直试图解决这个问题一段时间无济于事。当我尝试运行&#34;使用引导类加载器找不到类时,我遇到了很多错误;没有可用的堆栈跟踪&#34;和&#34;无法实例化活动ComponentInf ...&#34;我也收到一些警告,例如&#34; ClassLoader引用未知路径:/ data / app / packagenamehere-1 / lib / x86_64和/ data / app / packagenamehere-1 / lib / arm&#34;即使在模拟器中运行
这是我的日志cat文件:
12-16 17:26:25.167 22237-22237/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
12-16 17:26:25.197 22237-22243/com.ubiquity.sciencefacts I/art: Ignoring second debugger -- accepting and dropping
12-16 17:26:25.203 22237-22243/com.ubiquity.sciencefacts I/art: Debugger is no longer active
12-16 17:26:25.211 22237-22243/com.ubiquity.sciencefacts W/art: Suspending all threads took: 7.324ms
12-16 17:26:25.235 22237-22237/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
12-16 17:26:25.248 22237-22237/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:26:25.254 22237-22237/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 17:26:27.065 22237-22237/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 22237 SIG: 9
12-16 17:33:18.574 24986-24986/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
12-16 17:33:18.662 24986-24986/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:33:18.673 24986-24986/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 17:33:20.800 24986-24986/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 24986 SIG: 9
12-16 17:34:15.668 25845-25845/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/arm
12-16 17:34:15.687 25845-25845/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 17:34:15.693 25845-25845/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
12-16 18:00:52.080 2640-2640/com.ubiquity.sciencefacts I/art: Not late-enabling -Xcheck:jni (already on)
12-16 18:00:52.142 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
12-16 18:00:52.568 2640-2640/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-1/lib/x86_64
12-16 18:00:52.805 2640-2640/com.ubiquity.sciencefacts W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-16 18:00:53.993 2640-2646/com.ubiquity.sciencefacts W/art: Suspending all threads took: 633.582ms
12-16 18:00:54.020 2640-2716/com.ubiquity.sciencefacts D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-16 18:00:54.063 2640-2716/com.ubiquity.sciencefacts I/OpenGLRenderer: Initialized EGL, version 1.4
12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts E/EGL_emulation: tid 2716: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-16 18:00:54.091 2640-2716/com.ubiquity.sciencefacts W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f8b8ec90c40, error=EGL_BAD_MATCH
12-16 18:34:41.488 27869-27869/com.ubiquity.sciencefacts I/art: Late-enabling -Xcheck:jni
12-16 18:34:41.561 27869-27869/com.ubiquity.sciencefacts W/System: ClassLoader referenced unknown path: /data/app/com.ubiquity.sciencefacts-2/lib/arm
12-16 18:34:41.586 27869-27869/com.ubiquity.sciencefacts D/AndroidRuntime: Shutting down VM
12-16 18:34:41.589 27869-27869/com.ubiquity.sciencefacts E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ubiquity.sciencefacts, PID: 27869
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ubiquity.sciencefacts/com.ubiquity.sciencefacts.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.ubiquity.sciencefacts.MainActivity" on path: DexPathList[[zip file "/data/app/com.ubiquity.sciencefacts-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ubiquity.sciencefacts-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: com.ubiquity.sciencefacts.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
12-16 18:34:43.772 27869-27869/com.ubiquity.sciencefacts I/Process: Sending signal. PID: 27869 SIG: 9
重要更新:我将程序构建为apk并使用它将其安装在我的手机上,并且它没有问题。这是否意味着目标错误,如评论中所述?这会影响我的应用程序的分发(为了将来的参考,我实际上并没有分发这个)?
答案 0 :(得分:1)
关于您的错误,似乎缺少一些SDK。
如果安装了所有需要的SDK和SDK工具,请检查您的SDK管理器。
答案 1 :(得分:1)
在build.gradle中使用multidexEnabled:
defaultConfig {
multiDexEnabled true
}
清理,构建和运行
答案 2 :(得分:1)
在大多数情况下,只需cleaning
和rebuilding
该项目即可。
答案 3 :(得分:1)
尝试启用USB调试模式。 Here's手册
答案 4 :(得分:1)
当您使用连接到Android Studio的USB线调试/运行应用时,对于某些设备,当您卸载应用并尝试通过工作室再次运行它时,它会显示目标错误。
为此,卸载应用程序后,只需断开设备与USB电缆的连接,然后重新连接即可。
答案 5 :(得分:0)
您还可以检查Build.Gradle文件。 在你的情况下,它应该是这样的:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
确保通过将minifyEnabled设置为false来禁用proguard。这可能导致确保这不会导致其他错误;)