应用程序适用于模拟器但不适用于真实设备

时间:2014-02-25 14:37:58

标签: java android

我刚将我的应用程序上传到谷歌播放,然后将其安装在运行Android 2.3.5的Android手机上。无论如何,在模拟器上一切都运行良好,但在真实设备上启动时崩溃。有任何想法吗?这是代码。

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();
    }
}

提前致谢

修改

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

1 个答案:

答案 0 :(得分:0)

看起来你使用的方法没有在android&lt; = 2.3.5中实现。如果您删除@SuppressLint("NewApi"),则会看到它是哪一个。

我认为它适用于您的模拟器,因为您在那里使用了更高版本的sdk版本。