按主页按钮时应用关闭

时间:2013-09-25 02:05:33

标签: android forceclose

每当我按下应用程序上的主页按钮然后再返回时,应用程序似乎已停止。我真的不知道为什么因为我还没有做任何事情。所以这意味着唯一被执行的是我的oncreate,onresume,onpause和destroy。我没有在onresume,onpause和destroy上做任何事情。尚未开始服务或只是在oncreate中加载和初始化数据。另一件事是,它没有崩溃,只是停止或关闭。没有错误。我将与您分享我的onCreate按钮。也许你会看到导致这种情况发生的原因。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.layout_landstar_page);

        preferences = getSharedPreferences(pref_data, Context.MODE_PRIVATE);
        alertDialogBuilder = new AlertDialog.Builder(this);
        i = new Intent(this, BGService.class);

        shipmentAvailableLayout = ((LinearLayout)findViewById(R.id.shipment_available_layout));
        shipmentNotAvailableLayout = ((LinearLayout)findViewById(R.id.shipment_notavailable_layout));
        menu = ((RelativeLayout)findViewById(R.id.menu));
        declineBtn = ((Button)findViewById(R.id.declinebtn));
        acceptBtn = ((Button)findViewById(R.id.acceptbtn));
        callHelpDeskBtn = ((Button)findViewById(R.id.callhelpdeskbtn));
        menuBtn = ((ImageView)findViewById(R.id.menubtn));
        refreshBtn = ((ImageView)findViewById(R.id.refreshbtn));
        logoutBtn = ((ImageView)findViewById(R.id.logoutbtn));
        trackingMsgShipment = ((TextView)findViewById(R.id.tracking_message_shipment));

        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakeLock");

        registerReceiver(broadcastReceiver, new IntentFilter(BGService.BROADCAST_ACTION));

        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        ((Button)findViewById(R.id.acceptbtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    acceptBtn.setBackgroundResource(R.drawable.accept_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    acceptBtn.setBackgroundResource(R.drawable.accept_idle);

                    if(isNetworkConnected())
                    {
                        acceptLoad();

                    }
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((Button)findViewById(R.id.declinebtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    declineBtn.setBackgroundResource(R.drawable.decline_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    declineBtn.setBackgroundResource(R.drawable.decline_idle);
                    declineLoad();
                }
                return false;
            }
        });

        ((Button)findViewById(R.id.callhelpdeskbtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    callHelpDeskBtn.setBackgroundResource(R.drawable.btn_refresh_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    callHelpDeskBtn.setBackgroundResource(R.drawable.btn_refresh_idle);
                    if(isNetworkConnected())
                    {
                        displayProgressSpinner();
                    }
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.menubtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    if(menu.isShown())
                    {
                        menu.setVisibility(View.GONE);
                    }
                    else
                    {
                        menu.setVisibility(View.VISIBLE);
                    }
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.refreshbtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    menu.setVisibility(View.GONE);
                    if(isNetworkConnected())
                        displayProgressSpinner();
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.logoutbtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    menu.setVisibility(View.GONE);

                    try{
                        checkerTimer.cancel();
                        gpsTimer.cancel();
                        counterTimer2.cancel();
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.i("isAccept", preferences.getString("isAccepted",""));

                    alertDialogBuilder.setTitle("Message");
                    alertDialogBuilder
                    .setMessage("Clicking the log out button will stop your GPS and exit the application. Are you sure?")
                        .setCancelable(false)
                        .setNegativeButton("Logout",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                                SharedPreferences.Editor editor = preferences.edit();
                                editor.putString("isDriverLogin", "True");
                                editor.putString("driverPassword", driverPassword);
                                editor.putString("carrierId", carrierId);
                                editor.putString("CCTID", cctid);
                                editor.putString("shipment", entityShipment);
                                editor.putString("isAccepted", "");
                                editor.commit();
                                getApplicationContext().stopService(i);
                                finish();
                            }
                         })
                        .setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                            }
                          });


                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();

                }
                return false;
            }
        });


        Intent intent = getIntent();
        loadSaveDetails(intent);
    }

我的宣言:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    >
    <activity
        android:name="com.nesv.landstar.SplashScreen"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.nesv.landstar.Login"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <activity
        android:name="com.nesv.landstar.DriverLogin"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <activity
        android:name="com.nesv.landstar.LandstarPage"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <service android:name="com.nesv.landstar.BGService"></service>

</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.WAKE_LOCK" />

有一件事,这不会发生在有根的三星s3和普通的三星s4上。在S duos版本4.0上尝试并降低到姜饼。这件事发生了。

另一个问题。我试着从日食中安装它。这没有发生。这只发生在我尝试安装来自设备上bin文件夹的apk文件时。

2 个答案:

答案 0 :(得分:0)

覆盖onPause(),onStop()和onDestroy()(确保在每个中调用super.onXXX())并在每个中放入一个日志语句,以查看系统是否决定销毁您的活动,例如:因为系统内存很低。如果确实如此,则在返回活动时需要再次调用onCreate()。

开发工具&#39;应用程序有一个名为&#39;立即销毁活动的设置&#39;无论系统是否需要RAM,当您离开Activity时,它总会销毁一个Activity。

答案 1 :(得分:0)

尝试以下代码。

public class StartupActivity extends Activity {
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if (needStartApp()) {
                Intent i = new Intent(StartupActivity.this, MainActivity.class);
                startActivity(i);
            }

            finish();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            // this prevents StartupActivity recreation on Configuration changes 
            // (device orientation changes or hardware keyboard open/close).
            // just do nothing on these changes:
            super.onConfigurationChanged(null);
        }

        private boolean needStartApp() {
            final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

            if (!tasksInfo.isEmpty()) {
                final String ourAppPackageName = getPackageName();
                RunningTaskInfo taskInfo;
                final int size = tasksInfo.size();
                for (int i = 0; i < size; i++) {
                    taskInfo = tasksInfo.get(i);
                    if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
                        // continue application start only if there is the only Activity in the task
                        // (BTW in this case this is the StartupActivity)
                        return taskInfo.numActivities == 1;
                    }
                }
            } 

            return true;
        }

}

从此启动活动启动您的家庭活动。 在清单

<activity android:name=".StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>