如何让Android 2.2应用程序在android 4中运行? 这是一项伟大的重构工作吗?或者只是在项目中改变一些sdk设置?
顺便说一句:哪个更好的情况: - 适用于android 4的Android 2.2应用程序? - Android 4应用程序与谷歌兼容包?提前致谢?
编辑: 我问这个是因为我的应用程序在Android 2.2和2.3中运行良好但在android 4中崩溃。
StackTrace:
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);
}
}
经过一番挖掘后,我发现在VexLayout中重写的View类中的onFinishInflate()在运行android 4时没有被调用。任何想法?
答案 0 :(得分:1)
Android平台通常是向前兼容的。这意味着您可以为SDK 8(2.2)编写应用程序,它将在4.0上运行。
<强>更新强>
尝试检查其中一个视图(例如prevButton
或nextButton
)是否等于null,getContext
或Context.getString(int)
在方法中返回null
setButtonVisibility(int,int,int,int,int)
答案 1 :(得分:0)
Android具有向后兼容性,因此为Android 2.2编写的应用程序应该适用于android 4。
答案 2 :(得分:0)
有可能。我想如果你尝试在android 4中安装它,它将正常安装。但如果你反之亦然,那就不可能了。因为android 4提供了许多改进。