0

如何使 android 2.2 应用程序在 android 4 中运行?这是一项伟大的重构工作吗?还是只是在项目中更改一些 sdk 设置?

顺便说一句:哪种情况更好:-适用于 android 4 的适用于 android 2.2 的应用程序?- 带有来自 google 的兼容性包的 android 4 应用程序?

提前致谢?

编辑:我问这个是因为我的应用程序在 android 2.2 和 2.3 中运行良好,但在 android 4 中崩溃。

堆栈跟踪 :

E/AndroidRuntime(  660): FATAL EXCEPTION: main
E/AndroidRuntime(  660): java.lang.RuntimeException: Unable to start activity ComponentInfo{vex.android/vex.android.controllers.ControllerLoginView}: java.lang.NullPointerException
E/AndroidRuntime(  660):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(  660):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(  660):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(  660):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(  660):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  660):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  660):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(  660):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  660):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(  660):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(  660):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(  660):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  660): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  660):    at vex.android.layout.layout_titleheader.setButtonVisibility(layout_titleheader.java:163)
E/AndroidRuntime(  660):    at vex.android.layout.layout_titleheader.setButtonVisibility(layout_titleheader.java:155)
E/AndroidRuntime(  660):    at vex.android.controllers.ControllerLoginView.Initialize(ControllerLoginView.java:58)
E/AndroidRuntime(  660):    at vex.android.definition.VexActivity.onStart(VexActivity.java:154)
E/AndroidRuntime(  660):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
E/AndroidRuntime(  660):    at android.app.Activity.performStart(Activity.java:4475) 
E/AndroidRuntime(  660):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
E/AndroidRuntime(  660):    ... 11 more

layout_titleheader.java :

    package vex.android.layout;

    import vex.android.R;
    import vex.android.controllers.ControllerInfo;
    import vex.android.definition.VexLayout;
    import vex.android.definition.iVexParentable;
    import vex.android.definition.intentCode;
    import android.app.Activity;
    import android.app.Instrumentation;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.SystemClock;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;


    public class layout_titleheader extends VexLayout
    {
        /*** CONTROL POINTERS ***/
        Button nextButton;
        Button prevButton;
        ImageView infoButton;
        ImageButton upButton;
        ImageButton downButton;

        private Handler handler = new Handler(); 
        private Runnable upTask = new Runnable() { 
            public void run() {
                if(getContext() instanceof iVexParentable) {
                    ((iVexParentable)getContext()).onUpButton();
                }
                handler.postAtTime(this, SystemClock.uptimeMillis() +  100); 
            } 
        };
        private Runnable downTask = new Runnable() { 
            public void run() {
                if(getContext() instanceof iVexParentable) {
                    ((iVexParentable)getContext()).onDownButton();
                }
                handler.postAtTime(this, SystemClock.uptimeMillis() +  100); 
            } 
        };

        /**** CONSTRUCTORS ****/
        public layout_titleheader(Context context)
        {
            super(context);
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.layout_titleheader, this);
        }
        public layout_titleheader(Context context, AttributeSet attrs) {
            super(context, attrs);
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.layout_titleheader, this);
        }

        /**** INITIALIZERS ****/
        @Override
        public void Initialize()
        {
            infoButton.setOnTouchListener(new OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) 
                    {
                        showInfo();
                    }
                    return true;
                }
            });

            prevButton.setOnTouchListener(new OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Instrumentation i = new Instrumentation();
                                i.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                            }
                        }).start();
                    }
                    return true;
                }
            });

            nextButton.setOnTouchListener(new OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        if(getContext() instanceof iVexParentable)
                        {
                            ((iVexParentable)getContext()).onEditButton();
                        }
                    }
                    return true;
                }
            });
            upButton.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View view, MotionEvent motionevent) {
                    int action = motionevent.getAction();
                    if (action == MotionEvent.ACTION_DOWN) {
                        handler.removeCallbacks(upTask);
                        handler.postAtTime(upTask, SystemClock.uptimeMillis() + 100);
                    } else if (action == MotionEvent.ACTION_UP) {
                        handler.removeCallbacks(upTask);
                    }
                    return false;
                }
            });

            downButton.setOnTouchListener(new OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent motionevent) {
                    int action = motionevent.getAction();
                    if (action == MotionEvent.ACTION_DOWN) {
                        handler.removeCallbacks(downTask);
                        handler.postAtTime(downTask, SystemClock.uptimeMillis() + 100);
                    } else if (action == MotionEvent.ACTION_UP) {
                        handler.removeCallbacks(downTask);
                    }
                    return false;
                }
            });
        }

        @Override
        public void InitializeControls()
        {
            infoButton = (ImageView)findViewById(R.id.infoButton);
            prevButton = (Button)findViewById(R.id.prevButton);
            nextButton = (Button)findViewById(R.id.nextButton);
            upButton = (ImageButton)findViewById(R.id.upButton);
            downButton = (ImageButton)findViewById(R.id.downButton);
        }

        /**** LOCAL METHODS ****/
        public void showInfo()
        {
            if(getContext() instanceof Activity)
            {
                Intent intent = new Intent(getContext(), ControllerInfo.class);
                ((Activity)getContext()).startActivityForResult(intent, intentCode.INFO_DOSTART);
            }
        }
        public void setButtonVisibility( int previousButton, int editButton, int helpButton)
        {
            setButtonVisibility(previousButton, editButton, helpButton, View.INVISIBLE, View.INVISIBLE);
        }

        public void setButtonVisibility( int previousButton, int editButton, int helpButton, int upButtonVisibility, int downButtonVisibility)
        {
            if (View.VISIBLE == previousButton) {// fixes issue of translation not correctly displayed
                prevButton.setText(getContext().getString(R.string.Back));
            }
            prevButton.setVisibility(previousButton);
            nextButton.setVisibility(editButton);
            infoButton.setVisibility(helpButton);
            upButton.setVisibility(upButtonVisibility);
            downButton.setVisibility(downButtonVisibility);
        }

        public void setPreviousButtonText(int id) {
            prevButton.setText(getContext().getString(id));
        }

        public void setEditButtonName(int resId)
        {
            nextButton.setText(resId);
        }
    }

经过一番挖掘,我发现在运行 android 4 时不会调用在 VexLayout 中覆盖的 View 类的 onFinishInflate() 。知道吗?

4

3 回答 3

1

Android 平台一般是向前兼容的。这意味着您可以为 SDK 8 (2.2) 编写您的应用程序,它将在 4.0 上运行。

更新: 尝试检查视图之一(例如prevButtonnextButton)是否等于 null,getContextContext.getString(int)在方法 setButtonVisibility(int, int, int, int, int) 中返回 null

于 2012-04-06T11:29:19.533 回答
0

Android 具有向后兼容性,因此为 android 2.2 编写的应用程序应该可以在 android 4 上运行。

于 2012-04-06T11:28:30.063 回答
0

有可能的。我想如果你尝试在 android 4 中安装它,它会正常安装。但是如果你反过来尝试,那就不可能了。因为 android 4 提供了许多高级更改。

于 2012-04-06T11:29:16.523 回答