【发布时间】:2014-03-13 11:03:26
【问题描述】:
所有我在模拟器中运行的应用程序,包括从 Google 应用程序页面下载的应用程序都会收到 thisApp 已意外停止错误。
http://developer.android.com/samples/BasicAccessibility/index.html
有什么想法吗?我应该重新安装 Eclipse 吗?我应该在手机上而不是模拟器上运行吗?
Logcat:
03-13 10:44:34.158: D/AndroidRuntime(372): Shutting down VM
03-13 10:44:34.158: W/dalvikvm(372): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-13 10:44:34.198: E/AndroidRuntime(372): FATAL EXCEPTION: main
03-13 10:44:34.198: E/AndroidRuntime(372): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.basicaccessibility/com.example.android.basicaccessibility.MainActivity}: java.lang.ClassNotFoundException: com.example.android.basicaccessibility.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.android.basicaccessibility-2.apk]
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.os.Looper.loop(Looper.java:126)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-13 10:44:34.198: E/AndroidRuntime(372): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:44:34.198: E/AndroidRuntime(372): at java.lang.reflect.Method.invoke(Method.java:491)
03-13 10:44:34.198: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-13 10:44:34.198: E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-13 10:44:34.198: E/AndroidRuntime(372): at dalvik.system.NativeStart.main(Native Method)
03-13 10:44:34.198: E/AndroidRuntime(372): Caused by: java.lang.ClassNotFoundException: com.example.android.basicaccessibility.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.android.basicaccessibility-2.apk]
03-13 10:44:34.198: E/AndroidRuntime(372): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
03-13 10:44:34.198: E/AndroidRuntime(372): at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
03-13 10:44:34.198: E/AndroidRuntime(372): at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
03-13 10:44:34.198: E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-13 10:44:34.198: E/AndroidRuntime(372): ... 11 more*
sample_main.xml:
http://www.apache.org/licenses/LICENSE-2.0除非适用法律要求或书面同意,否则软件 根据许可分发是在“原样”基础上分发的, 没有任何形式的保证或条件,无论是明示或 默示。请参阅许可证以了解具体的管理语言 许可下的权限和限制。 -->
<!-- Notice the presence of nextFocusDown/nextFocusUp on the elements below. You can also use nextFocusLeft/nextFocusRight. This tells the system in what order elements should be navigated through. If not present, the system will make a guess based on element location in the layout. --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Buttons" android:id="@+id/buttonsLabel" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:nextFocusDown="@+id/composeButton"/> <!-- This is a regular, text-based button. No contentDescription is needed, since the text field sufficiently describes the action performed. --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/composeButtonLabel" android:id="@+id/composeButton" android:layout_below="@+id/buttonsLabel" android:layout_alignLeft="@+id/buttonsLabel" android:nextFocusUp="@+id/buttonsLabel" android:nextFocusDown="@+id/checkboxesLabel" /> <!-- The next two buttons are different types of image-based buttons. --> <!-- BEGIN_INCLUDE (image_content_description) --> <!-- Adding a contentDescription is needed for accessibility, since no text is present. Since the contentDescription is read verbatim, you may want to be a bit more descriptive than usual, such as adding "button" to the end of your description, if appropriate. --> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/discardButton" android:layout_alignTop="@+id/composeButton" android:layout_toRightOf="@+id/composeButton" android:src="@drawable/ic_action_discard" android:layout_alignBottom="@+id/composeButton" android:contentDescription="@string/discardButtonDescription" android:scaleType="fitCenter" android:nextFocusUp="@+id/buttonsLabel" android:nextFocusDown="@+id/checkboxesLabel" /> <!-- END_INCLUDE (image_content_description) --> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/infoButton" android:layout_alignTop="@+id/discardButton" android:layout_toRightOf="@+id/discardButton" android:src="@drawable/ic_action_info" android:layout_alignBottom="@+id/discardButton" android:layout_alignRight="@+id/hyperspaceCheckbox" android:scaleType="fitCenter" android:background="?android:selectableItemBackground" android:padding="5dp" android:contentDescription="@string/infoButtonDescription" android:nextFocusUp="@+id/buttonsLabel" android:nextFocusDown="@+id/checkboxesLabel" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkboxesLabel" android:id="@+id/checkboxesLabel" android:layout_below="@+id/composeButton" android:layout_alignLeft="@+id/composeButton" android:nextFocusUp="@+id/composeButton" android:nextFocusDown="@+id/jetpackCheckbox" /> <!-- Like a text-based button, checkboxes with text will often work correctly as-is. If your checkboxes do not have a text attribute, you will need to add a contentDescriptoin. --> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/jetpackCheckboxLabel" android:id="@+id/jetpackCheckbox" android:layout_below="@+id/checkboxesLabel" android:layout_alignLeft="@+id/checkboxesLabel" android:checked="false" android:nextFocusUp="@+id/checkboxesLabel" android:nextFocusDown="@+id/hyperspaceCheckbox" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hyperspaceCheckboxLabel" android:id="@+id/hyperspaceCheckbox" android:layout_below="@+id/jetpackCheckbox" android:layout_alignLeft="@+id/jetpackCheckbox" android:checked="false" android:nextFocusUp="@+id/jetpackCheckbox" android:nextFocusDown="@+id/imagesAndTextLabel" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/imagesAndTextLabel" android:id="@+id/imagesAndTextLabel" android:layout_below="@+id/hyperspaceCheckbox" android:layout_alignLeft="@+id/hyperspaceCheckbox" android:nextFocusUp="@+id/hyperspaceCheckbox" android:nextFocusDown="@+id/partlyCloudImage" /> <!-- Images should have a contentDescription if they convey any meaningful information. Images that are purely decorative may not need a contentDescription, however. --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/partlyCloudyImage" android:layout_below="@+id/imagesAndTextLabel" android:layout_alignLeft="@+id/imagesAndTextLabel" android:src="@drawable/partly_cloudy" android:contentDescription="@string/partlyCloudyDescription" android:layout_alignRight="@+id/discardButton" android:nextFocusUp="@+id/imagesAndTextLabel" android:nextFocusDown="@+id/customViewLabel" /> <!-- TextViews are typically self describing, so do not need extra modifications. --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/temperature" android:textSize="60sp" android:id="@+id/temperatureText" android:layout_alignTop="@+id/partlyCloudyImage" android:layout_toRightOf="@+id/partlyCloudyImage" android:layout_alignBottom="@+id/partlyCloudyImage" android:gravity="center_vertical" android:nextFocusUp="@+id/imagesAndTextLabel" android:nextFocusDown="@+id/customViewLabel" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/customViewLabel" android:id="@+id/customViewLabel" android:layout_below="@+id/partlyCloudyImage" android:layout_alignLeft="@+id/partlyCloudyImage" android:nextFocusUp="@+id/partlyCloudImage" android:nextFocusDown="@+id/dialView" /> <!-- Custom views require additonal code changes. See DialView.java for more details. --> <com.example.android.basicaccessibility.DialView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/dialView" android:layout_below="@+id/customViewLabel" android:layout_alignLeft="@+id/partlyCloudyImage" android:nextFocusUp="@+id/customViewLabel" /> </RelativeLayout> </ScrollView> </LinearLayout>
activity_main.xml:
http://www.apache.org/licenses/LICENSE-2.0除非适用法律要求或书面同意,否则软件 根据许可分发是在“原样”基础上分发的, 没有任何形式的保证或条件,无论是明示或 默示。请参阅许可证以了解具体的管理语言 许可下的权限和限制。 --><LinearLayout style="@style/Widget.SampleMessageTile" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView style="@style/Widget.SampleMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/horizontal_page_margin" android:layout_marginRight="@dimen/horizontal_page_margin" android:layout_marginTop="@dimen/vertical_page_margin" android:layout_marginBottom="@dimen/vertical_page_margin" android:text="@string/intro_message" /> </LinearLayout> </LinearLayout>
MainActivity.java:
/* * 版权所有 (C) 2013 Android 开源项目 * * 根据 Apache 许可证 2.0 版(“许可证”)获得许可; * 你 除非遵守许可,否则不得使用此文件。 * 你 可以在 * *
http://www.apache.org/licenses/LICENSE-2.0 * * 除非要求 适用法律或书面同意,软件 * 分发下 许可证按“原样”分发,* 不提供任何保证 或任何明示或暗示的条件。 * 见 特定语言管理权限的许可和 * 许可证下的限制。 */包 com.example.android.basicaccessibility;
导入android.app.Activity;导入android.os.Bundle;
/** * 基本活动类。 * *
负责渲染 布局,并显示一些祝酒词以提供按钮反馈。 * 这门课没有什么特别有趣的。所有的 有趣的东西在 * res/layout/activity_main.xml 和 {@link 拨号视图}。 */ 公共类 MainActivity 扩展 Activity {
/** * Standard onCreate() implementation. Sets R.layout.activity_main as the layout. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_main); } }
DialView.java:
包 com.example.android.basicaccessibility;
导入 android.annotation.TargetApi;导入android.content.Context; 导入android.graphics.Canvas;导入android.graphics.Color;进口 android.graphics.Paint;导入android.os.Build;进口 android.util.AttributeSet;导入android.view.View;进口 android.view.accessibility.AccessibilityEvent;
/** * 自定义视图来展示可访问性。 * *
这个视图 不使用任何框架小部件,因此没有任何可访问性 功能 * 自动。相反,我们使用 {@link android.view.accessibility.AccessibilityEvent} 提供 * 操作系统的可访问性提示。 * *
例如,如果 TalkBack 是 启用后,用户将能够收到语音反馈,因为他们 * 与此视图交互。 * *
更一般地说,这个视图呈现一个 多位置“拨号”,可用于选择一个值 * 1 之间 4. 每按一次转盘,下一个位置将是 选定(模 * 最大位置数)。 */ 公开课 拨号视图扩展视图 { 私有静态 int SELECTION_COUNT = 4;
private static float FONT_SIZE = 40f; private float mWidth; private float mHeight; private float mWidthPadded; private float mHeightPadded; private Paint mTextPaint; private Paint mDialPaint; private float mRadius; private int mActiveSelection; /** * Constructor that is called when inflating a view from XML. This is called * when a view is being constructed from an XML file, supplying attributes * that were specified in the XML file. * * <p>In our case, this constructor just calls init(). * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @see #View(android.content.Context, android.util.AttributeSet, int) */ public DialView(Context context, AttributeSet attrs) { super(context, attrs); init(); } /** * Helper method to initialize instance variables. Called by constructor. */ private void init() { // Paint styles used for rendering are created here, rather than at render-time. This // is a performance optimization, since onDraw() will get called frequently. mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.BLACK); mTextPaint.setStyle(Paint.Style.FILL_AND_STROKE); mTextPaint.setTextAlign(Paint.Align.CENTER); mTextPaint.setTextSize(FONT_SIZE); mDialPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mDialPaint.setColor(Color.GRAY); // Initialize current selection. This will store where the dial's "indicator" is pointing. mActiveSelection = 0; // Setup onClick listener for this view. Rotates between each of the different selection // states on each click. // // Notice that we call sendAccessibilityEvent here. Some AccessibilityEvents are generated // by the system. However, custom views will typically need to send events manually as the // user interacts with the view. The type of event sent will vary, depending on the nature // of the view and how the user interacts with it. // // In this case, we are sending TYPE_VIEW_SELECTED rather than TYPE_VIEW_CLICKED, because // clicking on this view selects a new value. // // We will give our AccessibilityEvent further information about the state of the view in // onPopulateAccessibilityEvent(), which will be called automatically by the system // for each AccessibilityEvent. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Rotate selection to the next valid choice. mActiveSelection = (mActiveSelection + 1) % SELECTION_COUNT; // Send an AccessibilityEvent, since the user has interacted with the view. sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); // Redraw the entire view. (Inefficient, but this is sufficient for demonstration // purposes.) invalidate(); } }); } /** * This is where a View should populate outgoing accessibility events with its text content. * While this method is free to modify event attributes other than text content, doing so * should normally be performed in * {@link #onInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent)}. * <p/> * <p>Note that the behavior of this method will typically vary, depending on the type of * accessibility event is passed into it. The allowed values also very, and are documented * in {@link android.view.accessibility.AccessibilityEvent}. * <p/> * <p>Typically, this is where you'll describe the state of your custom view. You may also * want to provide custom directions when the user has focused your view. * * @param event The accessibility event which to populate. */ // BEGIN_INCLUDE (on_populate_accessibility_event) @Override @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void onPopulateAccessibilityEvent(AccessibilityEvent event) { super.onPopulateAccessibilityEvent(event); // Detect what type of accessibility event is being passed in. int eventType = event.getEventType(); // Common case: The user has interacted with our view in some way. State may or may not // have been changed. Read out the current status of the view. // // We also set some other metadata which is not used by TalkBack, but could be used by // other TTS engines. if (eventType == AccessibilityEvent.TYPE_VIEW_SELECTED || eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) { event.getText().add("Mode selected: " + Integer.toString(mActiveSelection + 1) + "."); event.setItemCount(SELECTION_COUNT); event.setCurrentItemIndex(mActiveSelection); } // When a user first focuses on our view, we'll also read out some simple instructions to // make it clear that this is an interactive element. if (eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED) { event.getText().add("Tap to change."); } } // END_INCLUDE (on_populate_accessibility_event) /** * This is called during layout when the size of this view has changed. If * you were just added to the view hierarchy, you're called with the old * values of 0. * * <p>This is where we determine the drawing bounds for our custom view. * * @param w Current width of this view. * @param h Current height of this view. * @param oldw Old width of this view. * @param oldh Old height of this view. */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // Account for padding float xPadding = (float) (getPaddingLeft() + getPaddingRight()); float yPadding = (float) (getPaddingTop() + getPaddingBottom()); // Compute available width/height mWidth = w; mHeight = h; mWidthPadded = w - xPadding; mHeightPadded = h - yPadding; mRadius = (float) (Math.min(mWidth, mHeight) / 2 * 0.8); } /** * Render view content. * * <p>We render an outer grey circle to serve as our "dial", and then render a smaller black * circle to server as our indicator. The position for the indicator is determined based * on mActiveSelection. * * @param canvas the canvas on which the background will be drawn */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Draw dial canvas.drawCircle(mWidth / 2, mHeight / 2, (float) mRadius, mDialPaint); // Draw text labels final float labelRadius = mRadius + 10; for (int i = 0; i < SELECTION_COUNT; i++) { float[] xyData = computeXYForPosition(i, labelRadius); float x = xyData[0]; float y = xyData[1]; canvas.drawText(Integer.toString(i + 1), x, y, mTextPaint); } // Draw indicator mark final float markerRadius = mRadius - 35; float[] xyData = computeXYForPosition(mActiveSelection, markerRadius); float x = xyData[0]; float y = xyData[1]; canvas.drawCircle(x, y, 20, mTextPaint); } /** * Compute the X/Y-coordinates for a label or indicator, given the position number and radius * where the label should be drawn. * * @param pos Zero based position index * @param radius Radius where label/indicator is to be drawn. * @return 2-element array. Element 0 is X-coordinate, element 1 is Y-coordinate. */ private float[] computeXYForPosition(final int pos, final float radius) { float[] result = new float[2]; Double startAngle = Math.PI * (9 / 8d); // Angles are in radiansq Double angle = startAngle + (pos * (Math.PI / 4)); result[0] = (float) (radius * Math.cos(angle)) + (mWidth / 2); result[1] = (float) (radius * Math.sin(angle)) + (mHeight / 2); return result; } }
MainActivity 清单
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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>
【问题讨论】:
-
你更新了吗?