我查找了其他类似的问题,但找不到解决方案,所以我在这里。我是Java和Android编程的初学者,尽管我对C语言有很好的了解,对C++和Python有很好的了解,还有一些更具体的程序,如Mathematica和ROOT。
我尝试制作一个非常简单的Android应用程序:显示一个数字和两个按钮,可以提高或减少数字,但是我遇到了大量错误。如果你有时间帮助我,我对第一次影响感到非常气馁。
这是我.xml文件:
<RelativeLayout 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:background="@style/AppTheme"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".StartActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text1"
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:id="@+id/bSub"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/button2"
android:textSize="20sp" />
<Button
android:id="@+id/bAdd"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvDisplay"
android:layout_below="@+id/tvDisplay"
android:layout_marginTop="30dp"
android:text="@string/button1"
android:textSize="20sp" />
</RelativeLayout>
这是我.java文件:
package com.appdiprova.giovanni;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartActivity extends Activity {
int counter;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
counter=0;
add= (Button) findViewById(R.id.bAdd);
sub= (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter=counter+1;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter=counter-1;
display.setText("Your total is " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.start, menu);
return true;
}
}
当我运行程序时,我在 LogCat 中得到了一长串错误,其中第一个告诉我致命异常 MAIN。
我该如何解决这个问题?谢谢
编辑对不起,我也会发布清单和 LogCat
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appdiprova.giovanni"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.appdiprova.giovanni.StartActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
好吧,我很困惑,因为现在我不再得到致命的ECEPTION,但仍然有很多错误:
11-15 08:39:35.410: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.410: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.420: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.430: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.430: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:35.430: E/SoundPool(370): error loading /system/media/audio/ui/KeypressStandard.ogg
11-15 08:39:35.430: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
11-15 08:39:35.450: E/SoundPool(370): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-15 08:39:35.450: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
11-15 08:39:35.450: E/SoundPool(370): error loading /system/media/audio/ui/KeypressDelete.ogg
11-15 08:39:35.450: I/ActivityManager(370): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.appdiprova.giovanni/.StartActivity} from pid 546
11-15 08:39:35.460: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
11-15 08:39:35.460: E/SoundPool(370): error loading /system/media/audio/ui/KeypressReturn.ogg
11-15 08:39:35.470: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
11-15 08:39:35.470: E/SoundPool(370): error loading /system/media/audio/ui/KeypressInvalid.ogg
11-15 08:39:35.470: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
11-15 08:39:35.470: W/AudioService(370): onLoadSoundEffects(), Error -1 while loading samples
11-15 08:39:35.560: E/gralloc_goldfish(50): gralloc_alloc: Mismatched usage flags: 246 x 410, usage 333
11-15 08:39:35.560: W/GraphicBufferAllocator(50): alloc(246, 410, 1, 00000333, ...) failed -22 (Invalid argument)
11-15 08:39:35.560: E/(50): GraphicBufferAlloc::createGraphicBuffer(w=246, h=410) failed (Invalid argument), handle=0x0
11-15 08:39:35.560: E/BufferQueue(370): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
11-15 08:39:35.570: W/WindowManager(370): Screenshot failure taking screenshot for (246x410) to layer 21005
11-15 08:39:35.780: D/LightsService(370): Excessive delay setting light: 392ms
11-15 08:39:35.850: D/dalvikvm(924): Not late-enabling CheckJNI (already on)
11-15 08:39:35.870: I/ActivityManager(370): Start proc com.appdiprova.giovanni for activity com.appdiprova.giovanni/.StartActivity: pid=924 uid=10051 gids={50051}
11-15 08:39:36.610: D/AndroidRuntime(924): Shutting down VM
11-15 08:39:36.610: W/dalvikvm(924): threadid=1: thread exiting with uncaught exception (group=0xb3ad1b90)
11-15 08:39:36.630: E/AndroidRuntime(924): FATAL EXCEPTION: main
11-15 08:39:36.630: E/AndroidRuntime(924): Process: com.appdiprova.giovanni, PID: 924
11-15 08:39:36.630: E/AndroidRuntime(924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appdiprova.giovanni/com.appdiprova.giovanni.StartActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.os.Looper.loop(Looper.java:137)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-15 08:39:36.630: E/AndroidRuntime(924): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 08:39:36.630: E/AndroidRuntime(924): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 08:39:36.630: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 08:39:36.630: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-15 08:39:36.630: E/AndroidRuntime(924): at dalvik.system.NativeStart.main(Native Method)
11-15 08:39:36.630: E/AndroidRuntime(924): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-15 08:39:36.630: E/AndroidRuntime(924): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-15 08:39:36.630: E/AndroidRuntime(924): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.Activity.setContentView(Activity.java:1928)
11-15 08:39:36.630: E/AndroidRuntime(924): at com.appdiprova.giovanni.StartActivity.onCreate(StartActivity.java:20)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.Activity.performCreate(Activity.java:5243)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-15 08:39:36.630: E/AndroidRuntime(924): ... 11 more
11-15 08:39:36.630: E/AndroidRuntime(924): Caused by: java.lang.reflect.InvocationTargetException
11-15 08:39:36.630: E/AndroidRuntime(924): at java.lang.reflect.Constructor.constructNative(Native Method)
11-15 08:39:36.630: E/AndroidRuntime(924): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
11-15 08:39:36.630: E/AndroidRuntime(924): ... 23 more
11-15 08:39:36.630: E/AndroidRuntime(924): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060001 a=-1 r=0x7f060001}
11-15 08:39:36.630: E/AndroidRuntime(924): at android.content.res.Resources.loadDrawable(Resources.java:2068)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.View.<init>(View.java:3545)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.View.<init>(View.java:3475)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.view.ViewGroup.<init>(ViewGroup.java:464)
11-15 08:39:36.630: E/AndroidRuntime(924): at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
11-15 08:39:36.630: E/AndroidRuntime(924): ... 26 more
11-15 08:39:36.650: W/ActivityManager(370): Force finishing activity com.appdiprova.giovanni/.StartActivity
11-15 08:39:37.050: I/WindowManager(370): Screenshot max retries 4 of Token{b40edef0 ActivityRecord{b40edd90 u0 com.appdiprova.giovanni/.StartActivity t3 f}} appWin=Window{b41b27f0 u0 Starting com.appdiprova.giovanni} drawState=4
11-15 08:39:37.050: W/WindowManager(370): Screenshot failure taking screenshot for (480x800) to layer 21010
11-15 08:39:37.560: W/ActivityManager(370): Activity pause timeout for ActivityRecord{b40edd90 u0 com.appdiprova.giovanni/.StartActivity t3 f}
11-15 08:39:37.900: W/EGL_emulation(546): eglSurfaceAttrib not implemented
11-15 08:39:38.640: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.640: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.640: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.640: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.640: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.650: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.650: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.650: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.650: E/SoundPool(370): error loading /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.650: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
11-15 08:39:38.680: E/SoundPool(370): error loading /system/media/audio/ui/KeypressStandard.ogg
11-15 08:39:38.680: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
11-15 08:39:38.710: E/SoundPool(370): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-15 08:39:38.710: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
11-15 08:39:38.720: E/SoundPool(370): error loading /system/media/audio/ui/KeypressDelete.ogg
11-15 08:39:38.720: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
11-15 08:39:38.720: E/SoundPool(370): error loading /system/media/audio/ui/KeypressReturn.ogg
11-15 08:39:38.720: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
11-15 08:39:38.730: E/SoundPool(370): error loading /system/media/audio/ui/KeypressInvalid.ogg
11-15 08:39:38.730: W/AudioService(370): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
11-15 08:39:38.730: W/AudioService(370): onLoadSoundEffects(), Error -1 while loading samples
11-15 08:39:38.800: I/Process(924): Sending signal. PID: 924 SIG: 9
11-15 08:39:38.800: W/InputMethodManagerService(370): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b41dee38 attribute=null, token = android.os.BinderProxy@b4048d78
11-15 08:39:38.830: I/ActivityManager(370): Process com.appdiprova.giovanni (pid 924) has died.
11-15 08:39:42.300: D/MobileDataStateTracker(370): default: setPolicyDataEnable(enabled=true)
11-15 08:39:43.530: E/NetdConnector(370): NDC Command {47 bandwidth setiquota eth0 9223372036854775807} took too long (782ms)
我认为问题是您的RelativeLayout
的android:background
属性:
android:background="@style/AppTheme"
安卓告诉你
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060001 a=-1 r=0x7f060001}
系统期望background
资源是Drawable
,而不是样式。使用 style
设置样式,或使用android:background
设置要用作背景的Drawable
:
style="@style/AppTheme"
更新:来自有关android:background
的文档:
用作背景的可绘制对象。这可以是 完整的可绘制资源(例如 PNG 图像、9 个补丁、XML 状态列表) 描述等),或纯色,如"#ff000000"(黑色)。
您可以在 Android 开发者指南中阅读有关样式和主题背景的更多信息。