Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序可以在模拟器上运行,但不能在真实设备上运行_Java_Android - Fatal编程技术网

Java 应用程序可以在模拟器上运行,但不能在真实设备上运行

Java 应用程序可以在模拟器上运行,但不能在真实设备上运行,java,android,Java,Android,我刚刚把我的应用程序上传到google play上,然后把它安装在我的安卓手机上,安卓手机运行的是安卓2.3.5。不管怎样,在模拟器上一切都运行得很好,但在真正的设备上启动时就崩溃了。有什么想法吗?这是密码 public class StopWatch extends Activity implements OnClickListener { //PROPERTIES USED THROUGHOUT CLASS private Random rand = new Random()

我刚刚把我的应用程序上传到google play上,然后把它安装在我的安卓手机上,安卓手机运行的是安卓2.3.5。不管怎样,在模拟器上一切都运行得很好,但在真正的设备上启动时就崩溃了。有什么想法吗?这是密码

public class StopWatch extends Activity implements OnClickListener {
    //PROPERTIES USED THROUGHOUT CLASS
    private Random rand = new Random();
    private TextView stopWatchC;
    private Button startButton,stopButton,resetButton;
    private RelativeLayout mainLayout;
    private Handler handle;
    private Handler backHand = new Handler();
    private boolean timerIsRunning;
    private boolean previouslyStarted;
    private long startTime;
    private long endTime;
    private long runTime;
    private long UPDATE_EVERY = 200;
    private int backgrounds[] = {
            R.drawable.woman_1,
            R.drawable.woman_2,
            R.drawable.woman_3,
            R.drawable.woman_4,
            R.drawable.woman_5,
            R.drawable.woman_6,
            R.drawable.woman_7,
            R.drawable.woman_8,
            R.drawable.woman_9
            }; 
    //END PROPERTY DECLARATIONS
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stopwatch);
        //Start our service
        startService(new Intent(this,StopwatchService.class));
       //SETUP BUTTON AND TEXTVIEWS
        stopWatchC = (TextView) findViewById(R.id.counter);
        startButton = (Button) findViewById(R.id.start_button);
        stopButton = (Button) findViewById(R.id.stop_button);
        resetButton = (Button) findViewById(R.id.reset);
        mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
        //Handles listening for clicks on our start,stop and reset buttons
        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
        resetButton.setOnClickListener(this);
        //Calls run method for changing backgrounds
        backHand.postDelayed(backgroundUpdate, 300);    

    }
    /**
     * Handles displaying the counter
     */
    public void SWCounterDisplay()
    {
    String display;
    long now;
    long difference;
    long secs;
    long mins;
    long hours;

    if(timerIsRunning == true)
    {
        now = System.currentTimeMillis();
    }else{
        now = endTime;
    }
    if(previouslyStarted == true){
    difference = runTime + (now - startTime);   
    }else{
    difference = now-startTime;
    }
    //No negative numbers
    if(difference < 0)
    {
        difference = 0;
    }

    secs = difference/1000;
    mins = secs/60;
    hours = mins/60;
    secs = secs%60;
    mins = mins%60;

    display = String.format("%d", hours) + ":" +
              String.format("%02d",mins) + ":" +
              String.format("%02d", secs);

              stopWatchC.setText(display);

    }
    /**
     * Reset the timer
     */
    public void resetTimer()
    {
        timerIsRunning = false;
        previouslyStarted = false;
        stopButton.setEnabled(false);
        startButton.setEnabled(true);
        runTime = 0;
        SWCounterDisplay();
        handle.removeCallbacks(timerUpdate);
        handle = null;
    }
    /**
     * Starts the stop watch 
     */
    public void startTimer()
    {
        timerIsRunning = true;
        stopButton.setEnabled(timerIsRunning);
        startButton.setEnabled(false);
        if(!previouslyStarted){
        previouslyStarted = true;   
        runTime = 0;
        }
        startTime = System.currentTimeMillis();
        //Create new handler
        handle = new Handler();
        handle.postDelayed(timerUpdate, UPDATE_EVERY);  

    }
    /**
     * Stops the timer
     */
    public void stopTimer()
    {
        timerIsRunning = false;
        stopButton.setEnabled(timerIsRunning);
        startButton.setEnabled(true);
        endTime = System.currentTimeMillis();
        runTime += endTime-startTime;
        handle.removeCallbacks(timerUpdate);
        handle = null;
    }
    /**
     * Handles any onClick events
     */
    @Override
    public void onClick(View v) {

        if(v == startButton)
        {
            startTimer();
        }else if(v == stopButton)
        {
            stopTimer();
        }else if(v == resetButton)
        {
            resetTimer();
        }
    }

    /**
     * Changes the background every 20 Seconds
     */
    private Runnable backgroundUpdate = new Runnable(){

        @Override
        public void run() {
          mainLayout.setBackgroundResource(backgrounds[rand.nextInt(backgrounds.length)]);
          backHand.postDelayed(this, 60000);
        }

    };
    /**
     * Handles updating the timer
     */
    private Runnable timerUpdate = new Runnable(){

        @Override
        public void run() {
            SWCounterDisplay(); 
            if(handle != null){
            handle.postDelayed(this, UPDATE_EVERY); 
            }

        }

    };
    /**
     * Call run method if timer is still running 
     */
    public void onStart()
    {
        super.onStart();
        if(timerIsRunning == true)
        {
            handle = new Handler();
            handle.postDelayed(timerUpdate, UPDATE_EVERY);  
        }
    }
    /**
     * Stop the timer if timer is still running
     */
    public void onStop()
    {
        super.onStop();
        if(timerIsRunning == true)
        {
            handle.removeCallbacks(timerUpdate);
            handle = null;
        }
    }
    /**
     * Resume when the onResume method is called
     */
    public void onResume()
    {
        super.onResume();

        if(timerIsRunning == true){ 
        stopButton.setEnabled(true);
        startButton.setEnabled(false);
        }else{
        stopButton.setEnabled(false);
        startButton.setEnabled(true);
        }

        SWCounterDisplay();
    }

package com.webdeveloper93.stopwatch;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StopwatchService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    public int onStartCommand(Intent intent,int flags,int startId)
    {
        Log.d("StopwatchService:","SERVICE STARTED");
        super.onStartCommand(intent, flags, startId);
        return START_NOT_STICKY;
    }

    public void onDestroy()
    {
        Log.d("StopwatchService:","SERVICE DESTROYED");
        super.onDestroy();
    }
}

看起来您使用的是一种未在android中实现的方法。请将您的问题发布到logcat上好吗?“但在实际设备启动时立即崩溃”请显示崩溃错误消息(logcat)是否为android 2.3.5配置了仿真器?清单文件中的Min SDK是什么?如果没有logcat消息,就无法真正帮助您我没有usb连接设备进行调试模拟器中的log cat正常吗?即使它在那里工作得很好谢谢你我删除了这个方法现在我只需要等待我新上传的apk生效再次感谢tho:)
I/ActivityManager(  132): No longer want com.google.android.gsf.login (pid 1140): hidden #16
D/WifiService(  132): ACTION_BATTERY_CHANGED pluggedType: 0
I/ActivityManager(  132): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.webdeveloper93.stopwatch/.StopWatch } from pid 231
E/AndroidRuntime( 1186): FATAL EXCEPTION: main
E/AndroidRuntime( 1186): java.lang.NoSuchMethodError: android.os.StrictMode$VmPolicy$Builder.detectLeakedClosableObjects
E/AndroidRuntime( 1186): at com.webdeveloper93.stopwatch.StopWatch.onCreate(StopWatch.java:59)
E/AndroidRuntime( 1186): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 1186): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 1186): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 1186): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 1186): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 1186): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1186): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 1186): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1186): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1186): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
E/AndroidRuntime( 1186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
E/AndroidRuntime( 1186): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  132):   Force finishing activity com.webdeveloper93.stopwatch/.StopWatch
W/ActivityManager(  132): Activity pause timeout for HistoryRecord{408ff850 com.webdeveloper93.stopwatch/.StopWatch}
W/InputMethodManagerService(  132): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@405ca178
I/ActivityManager(  132): Process com.webdeveloper93.stopwatch (pid 1186) has died.
W/ActivityManager(  132): Service crashed 2 times, stopping: ServiceRecord{4078c080 com.webdeveloper93.stopwatch/.StopwatchService}
W/ActivityManager(  132): Activity destroy timeout for HistoryRecord{408ff850 com.webdeveloper93.stopwatch/.StopWatch}
I/ActivityManager(  132): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.jtb.alogcat/.LogActivity } from pid 231