2014-02-25 31 views
0

我刚上传我的应用程序到谷歌播放,然后安装在我的Android手机上运行Android 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(); 
    } 
} 

在此先感谢

编辑

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: [email protected] 
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 
+9

请你可以发布logcat与你的问题? –

+0

*“但在真实设备上启动时发生崩溃”*请显示崩溃错误消息(Logcat) – m0skit0

+2

是否为Android 2.3.5配置了仿真器?清单文件中的Min SDK是什么? – Tyler

回答

0

它看起来像您使用的不是Android中< = 2.3.5实现的方法。如果你删除@SuppressLint("NewApi"),你会看到它是哪一个。

我认为它在你的模拟器中工作,因为你在那里使用更高的sdk版本。

+0

谢谢你,我删除了现在的方法,我只需要等待我的新上传apk生效,再次感谢tho :) – user3325917

相关问题