有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Eclipse:Android Emulator在我点击按钮时崩溃

我已经设置了一个秒表应用程序,当我按下按钮时,它将开始计数。但是,当我点击按钮时,模拟器崩溃,使我被迫关闭。我已经用系统测试了实际代码。出来print()和代码部分工作;但是,如果有任何帮助,我们将不胜感激

主要活动:

package com.example.stopwatch;
package com.example.stopwatch;

import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.TextView;

public class MainActivity extends Activity {

    private ClockCounter mClockCounter = new ClockCounter();
    boolean ifPressed = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Set Views
        final TextView currentTime = (TextView) findViewById(R.id.textView1);
        Button startStopButton = (Button) findViewById(R.id.button1);

        startStopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //turns stopwatch on and off.
                ifPressed ^= true;

                //runs the loop
                try{
                    while(ifPressed){//this will only run if ifPressed is true

                          Thread.sleep(1000);//makes the loop wait 1 second

                          String time = mClockCounter.getTime();
                            //Set time to TextView

                            currentTime.setText(time);
                        }
                }catch(InterruptedException ex){
                    //idk lmao
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

ClockCounter方法就是计数器,以字符串形式返回时间

主要活动。xml(如果与此无关,请抱歉):

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="wrap_content"
    安卓:layout_height="match_parent" >

    <LinearLayout
        安卓:id="@+id/linearLayout1"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_centerVertical="true"
        安卓:weightSum="1" >

        <View
            安卓:id="@+id/view1"
            安卓:layout_width="0dp"
            安卓:layout_height="0dp"
            安卓:layout_weight="0.2" />

        <Button
            安卓:id="@+id/button1"
            安卓:layout_width="0dp"
            安卓:layout_height="wrap_content"
            安卓:layout_weight="0.6"
            安卓:text="@string/StartStop" />

        <View
            安卓:id="@+id/View2"
            安卓:layout_width="0dp"
            安卓:layout_height="0dp"
            安卓:layout_weight="0.2" />
    </LinearLayout>

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentBottom="true"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentRight="true"
        安卓:layout_below="@+id/linearLayout1"
        安卓:orientation="vertical"
        安卓:weightSum="1" >

        <View
            安卓:id="@+id/view3"
            安卓:layout_width="match_parent"
            安卓:layout_height="0dp"
            安卓:layout_weight="0.1" />

        <AnalogClock
            安卓:id="@+id/analogClock1"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_weight="0.8" />

        <DigitalClock
            安卓:id="@+id/digitalClock1"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:layout_weight="0.1"
            安卓:text="DigitalClock" />

    </LinearLayout>

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_above="@+id/linearLayout1"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentRight="true"
        安卓:layout_alignParentTop="true"
        安卓:orientation="vertical"
        安卓:weightSum="1" >

        <View
            安卓:id="@+id/view4"
            安卓:layout_width="0dp"
            安卓:layout_height="0dp"
            安卓:layout_weight="0.3" />

        <LinearLayout
            安卓:id="@+id/LinearLayout01"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:weightSum="1" >

            <View
                安卓:id="@+id/View01"
                安卓:layout_width="0dp"
                安卓:layout_height="0dp"
                安卓:layout_weight="0.2" />

            <TextView
                安卓:id="@+id/textView1"
                安卓:layout_width="wrap_content"
                安卓:layout_height="60dp"
                安卓:layout_weight="0.60"
                安卓:text="00:00:00"
                安卓:textSize="50dp"
                安卓:width="@dimen/TimerSize" />

            <View
                安卓:id="@+id/View02"
                安卓:layout_width="0dp"
                安卓:layout_height="0dp"
                安卓:layout_weight="0.2" />

        </LinearLayout>

        <View
            安卓:id="@+id/view5"
            安卓:layout_width="0dp"
            安卓:layout_height="0dp"
            安卓:layout_weight="0.3" />

    </LinearLayout>

</RelativeLayout>

控制台:

[2013-12-30 00:24:31 - StopWatch] ------------------------------
[2013-12-30 00:24:31 - StopWatch] Android Launch!
[2013-12-30 00:24:31 - StopWatch] adb is running normally.
[2013-12-30 00:24:31 - StopWatch] Performing com.example.stopwatch.MainActivity activity launch
[2013-12-30 00:24:31 - StopWatch] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Android41'
[2013-12-30 00:24:33 - StopWatch] Application already deployed. No need to reinstall.
[2013-12-30 00:24:33 - StopWatch] Starting activity com.example.stopwatch.MainActivity on device emulator-5554
[2013-12-30 00:24:35 - StopWatch] ActivityManager: Starting: Intent { act=安卓.intent.action.MAIN cat=[安卓.intent.category.LAUNCHER] cmp=com.example.stopwatch/.MainActivity }
[2013-12-30 00:24:35 - StopWatch] ActivityManager: Warning: Activity not started, its current task has been brought to the front

日志:

12-30 00:24:28.983: D/AndroidRuntime(732): >>>>>> AndroidRuntime START com.安卓.internal.os.RuntimeInit <<<<<<
12-30 00:24:28.983: D/AndroidRuntime(732): CheckJNI is ON
12-30 00:24:29.043: D/dalvikvm(732): Trying to load lib libjavacore.so 0x0
12-30 00:24:29.053: D/dalvikvm(732): Added shared lib libjavacore.so 0x0
12-30 00:24:29.114: D/dalvikvm(732): Trying to load lib libnativehelper.so 0x0
12-30 00:24:29.114: D/dalvikvm(732): Added shared lib libnativehelper.so 0x0
12-30 00:24:30.283: D/AndroidRuntime(732): Calling main entry com.安卓.commands.pm.Pm
12-30 00:24:30.343: D/AndroidRuntime(732): Shutting down VM
12-30 00:24:30.363: D/dalvikvm(732): GC_CONCURRENT freed 102K, 78% free 466K/2048K, paused 11ms+2ms, total 23ms
12-30 00:24:30.373: D/dalvikvm(732): Debugger has detached; object registry had 1 entries
12-30 00:24:31.094: D/AndroidRuntime(745): >>>>>> AndroidRuntime START com.安卓.internal.os.RuntimeInit <<<<<<
12-30 00:24:31.094: D/AndroidRuntime(745): CheckJNI is ON
12-30 00:24:31.145: D/dalvikvm(745): Trying to load lib libjavacore.so 0x0
12-30 00:24:31.154: D/dalvikvm(745): Added shared lib libjavacore.so 0x0
12-30 00:24:31.195: D/dalvikvm(745): Trying to load lib libnativehelper.so 0x0
12-30 00:24:31.195: D/dalvikvm(745): Added shared lib libnativehelper.so 0x0
12-30 00:24:32.284: D/AndroidRuntime(745): Calling main entry com.安卓.commands.am.Am
12-30 00:24:32.335: I/ActivityManager(161): START {act=安卓.intent.action.MAIN cat=[安卓.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.stopwatch/.MainActivity u=0} from pid 745
12-30 00:24:32.354: D/AndroidRuntime(745): Shutting down VM
12-30 00:24:32.374: D/dalvikvm(745): GC_CONCURRENT freed 102K, 77% free 489K/2048K, paused 1ms+3ms, total 20ms
12-30 00:24:32.374: D/dalvikvm(745): Debugger has detached; object registry had 1 entries
12-30 00:25:36.473: D/dalvikvm(246): GC_CONCURRENT freed 384K, 7% free 8538K/9159K, paused 19ms+8ms, total 69ms
12-30 00:26:00.256: W/BroadcastQueue(161): Timeout of broadcast BroadcastRecord{41518378 安卓.intent.action.TIME_TICK} - receiver=安卓.os.BinderProxy@415928c8, started 60044ms ago
12-30 00:26:00.256: W/BroadcastQueue(161): Receiver during timeout: BroadcastFilter{415a2238 ReceiverList{415a7b40 716 com.example.stopwatch/10046 remote:415928c8}}
12-30 00:26:00.454: I/Process(161): Sending signal. PID: 716 SIG: 3
12-30 00:26:00.454: I/dalvikvm(716): threadid=3: reacting to signal 3
12-30 00:26:00.573: I/dalvikvm(716): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:00.573: I/Process(161): Sending signal. PID: 161 SIG: 3
12-30 00:26:00.573: I/dalvikvm(161): threadid=3: reacting to signal 3
12-30 00:26:01.555: I/dalvikvm(161): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:01.555: I/Process(161): Sending signal. PID: 216 SIG: 3
12-30 00:26:01.555: I/dalvikvm(216): threadid=3: reacting to signal 3
12-30 00:26:01.724: I/dalvikvm(216): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:01.734: I/Process(161): Sending signal. PID: 246 SIG: 3
12-30 00:26:01.734: I/dalvikvm(246): threadid=3: reacting to signal 3
12-30 00:26:02.053: I/dalvikvm(246): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:02.907: D/dalvikvm(161): GC_CONCURRENT freed 627K, 12% free 11626K/13191K, paused 77ms+15ms, total 344ms
12-30 00:26:02.907: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 74ms
12-30 00:26:02.914: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 0ms
12-30 00:26:03.124: D/dalvikvm(161): GC_EXPLICIT freed 121K, 13% free 11509K/13191K, paused 27ms+13ms, total 212ms
12-30 00:26:03.844: E/ActivityManager(161): ANR in com.example.stopwatch
12-30 00:26:03.844: E/ActivityManager(161): Reason: Broadcast of Intent { act=安卓.intent.action.TIME_TICK flg=0x40000014 (has extras) }
12-30 00:26:03.844: E/ActivityManager(161): Load: 0.32 / 0.4 / 0.35
12-30 00:26:03.844: E/ActivityManager(161): CPU usage from 55285ms to 0ms ago:
12-30 00:26:03.844: E/ActivityManager(161):   0.8% 161/system_server: 0.3% user + 0.4% kernel / faults: 15 minor
12-30 00:26:03.844: E/ActivityManager(161):   0.6% 246/com.安卓.phone: 0.3% user + 0.2% kernel / faults: 50 minor
12-30 00:26:03.844: E/ActivityManager(161):   0.4% 716/com.example.stopwatch: 0.3% user + 0.1% kernel / faults: 56 minor
12-30 00:26:03.844: E/ActivityManager(161):   0% 216/com.安卓.systemui: 0% user + 0% kernel / faults: 8 minor
12-30 00:26:03.844: E/ActivityManager(161):   0% 12/pdflush: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   0% 34/rild: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   0% 45/adbd: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161): 2.4% TOTAL: 1.2% user + 1.1% kernel
12-30 00:26:03.844: E/ActivityManager(161): CPU usage from 2789ms to 3411ms later:
12-30 00:26:03.844: E/ActivityManager(161):   14% 161/system_server: 7.9% user + 6.3% kernel
12-30 00:26:03.844: E/ActivityManager(161):     17% 176/ActivityManager: 7.9% user + 9.5% kernel
12-30 00:26:03.844: E/ActivityManager(161):     1.5% 175/er.ServerThread: 1.5% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   1.3% 246/com.安卓.phone: 1.3% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):     1.3% 499/GsmDC-1: 1.3% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161): 60% TOTAL: 40% user + 20% kernel
12-30 00:26:04.184: I/Choreographer(161): Skipped 31 frames!  The application may be doing too much work on its main thread.
12-30 00:26:04.664: D/dalvikvm(161): GC_CONCURRENT freed 345K, 10% free 11997K/13191K, paused 75ms+77ms, total 761ms
12-30 00:26:04.674: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 673ms
12-30 00:26:04.844: D/dalvikvm(161): GC_FOR_ALLOC freed 439K, 12% free 11716K/13191K, paused 163ms, total 164ms

感谢所有能帮忙的人


共 (1) 个答案

  1. # 1 楼答案

    因为应用程序没有响应,所以会得到ANR(应用程序没有响应)

    不要在主线程上运行Thread.sleep(),因为它肯定会使应用程序没有响应,尤其是在连续循环中运行时。你需要重新设计你的代码。如果需要执行某种计时器,那么应该在不阻塞主线程的情况下执行

    关于这个话题有很多资源。检查这个答案,例如:Android - Run a thread repeatingly within a timer