android - 可 ScrollView 应用程序崩溃的 Titanium Android 问题

标签 android ios geolocation titanium scrollable

我使用的是可 ScrollView ,我正在动态地为其分配 View 。在 ios 中,代码工作正常,但在 android 中,我遇到了问题

这是崩溃日志

[ERROR] : TiApplication: (main) [1402,23369] Sending event: exception on thread: main msg:java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0; Titanium 3.2.3,2014/04/22 10:17,b958a70
[ERROR] : TiApplication: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
[ERROR] : TiApplication: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
[ERROR] : TiApplication: at java.util.ArrayList.get(ArrayList.java:304)
[ERROR] : TiApplication: at ti.modules.titanium.ui.widget.TiUIScrollableView$2.onPageScrolled(TiUIScrollableView.java:192)
[ERROR] : TiApplication: at android.support.v4.view.ViewPager.onPageScrolled(ViewPager.java:1712)
[ERROR] : TiApplication: at android.support.v4.view.ViewPager.pageScrolled(ViewPager.java:1633)
[ERROR] : TiApplication: at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:581)
[ERROR] : TiApplication: at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1600)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at org.appcelerator.titanium.view.TiCompositeLayout.onLayout(TiCompositeLayout.java:578)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at org.appcelerator.titanium.view.TiCompositeLayout.onLayout(TiCompositeLayout.java:578)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at org.appcelerator.titanium.view.TiCompositeLayout.onLayout(TiCompositeLayout.java:578)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
[ERROR] : TiApplication: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
[ERROR] : TiApplication: at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
[ERROR] : TiApplication: at android.view.View.layout(View.java:14128)
[ERROR] : TiApplication: at android.view.ViewGroup.layout(ViewGroup.java:4422)
[ERROR] : TiApplication: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1990)
[ERROR] : TiApplication: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1811)
[ERROR] : TiApplication: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1108)
[ERROR] : TiApplication: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4491)
[ERROR] : TiApplication: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:755)
[ERROR] : TiApplication: at android.view.Choreographer.doCallbacks(Choreographer.java:585)
[ERROR] : TiApplication: at android.view.Choreographer.doFrame(Choreographer.java:555)
[ERROR] : TiApplication: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:741)
[ERROR] : TiApplication: at android.os.Handler.handleCallback(Handler.java:615)
[ERROR] : TiApplication: at android.os.Handler.dispatchMessage(Handler.java:92)
[ERROR] : TiApplication: at android.os.Looper.loop(Looper.java:137)
[ERROR] : TiApplication: at android.app.ActivityThread.main(ActivityThread.java:4949)
[ERROR] : TiApplication: at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR] : TiApplication: at java.lang.reflect.Method.invoke(Method.java:511)
[ERROR] : TiApplication: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
[ERROR] : TiApplication: at dalvik.system.NativeStart.main(Native Method)

这是代码

for (var i = 0; i < resultsArray.length; i++) {

var resultViewOuter;

if (Ti.Platform.name === 'iPhone OS') {
    resultViewOuter = Ti.UI.createView({
        width : 280,
        height : Ti.UI.FILL,
        left : 2,
        right : 2,
        backgroundColor : 'white',
        tid : resultsArray[i].tid,
        tag : i,
    });
} else {

    resultViewOuter = Ti.UI.createView({
        width : 280,
        height : Ti.UI.FILL,
        left : leftValue + 'dp',
        // right : 2,
        backgroundColor :'green', //'transparent',
        tid : resultsArray[i].tid,
        tag : i,
    });
}
var resultView;
if (Ti.Platform.name === 'iPhone OS') {
    resultView = Ti.UI.createView({
        width : 280,
        height : Ti.UI.FILL,
        left : 0,
        right : 0,
        backgroundColor : 'white',
        tid : resultsArray[i].tid,
        tag : i,
        layout : 'vertical'
    });
} else {
    resultView = Ti.UI.createView({
        width : 280,
        height : Ti.UI.FILL,
        left : leftValue,
        right : 0,
        backgroundColor : 'white',
        tid : resultsArray[i].tid,
        tag : i,
        layout : 'vertical'
    });
}

var locationImage;
if (Ti.Platform.name === 'iPhone OS') {
    locationImage = Ti.UI.createImageView({
        //width : Ti.UI.FILL,
        //height : 'auto',
        //width: Ti.UI.FILL,
        width : '276dp',
        //height: PixelsToDPUnits(300),
        top : '0dp',
        image : 'index.jpg',
        borderWidth : '2dp',
        borderColor : 'white',
        touchEnabled : false,
    });
} else {
    locationImage = Ti.UI.createImageView({
        width : '276dp',
        //height: PixelsToDPUnits(300),
        top : '0dp',
        image : '/images/index.jpg',
        borderWidth : '2dp',
        borderColor : 'white',
        touchEnabled : false,
    });
}

//Ti.API.info(resultsArray[i].image_url);
downloadImage(locationImage, resultsArray[i].image_url);

var title = Ti.UI.createTextArea({
    color : 'black',
    font : {
        fontSize : 17,
        fontWeight : 'normal'
    },
    textAlign : 'left',
    value : resultsArray[i].headline,
    //top : '140dp',
    width : Ti.UI.FILL,
    // height : '14%',
    touchEnabled : false,
    backgroundColor : 'transparent',
    left : '10dp',
    right : '10dp',
});
var description = Ti.UI.createTextArea({

    color : 'black',
    font : {
        fontSize : 12,
        fontWeight : 'gray'
    },
    textAlign : 'left',
    value : resultsArray[i].dek,
    width : Ti.UI.FILL,
    // height : '19.5%',
    //top : '190dp',
    touchEnabled : false,
    backgroundColor : 'transparent',
    left : '10dp',
    right : '10dp',
});
if (Ti.Platform.name === 'iPhone OS') {
    //do nothing
} else {
    title.softKeyboardOnFocus = Titanium.UI.Android.SOFT_KEYBOARD_HIDE_ON_FOCUS;
    description.softKeyboardOnFocus = Titanium.UI.Android.SOFT_KEYBOARD_HIDE_ON_FOCUS;
}
var moreImage;
if (Ti.Platform.name === 'iPhone OS') {
    moreImage = Ti.UI.createImageView({
        width : Ti.UI.SIZE,
        height : Ti.UI.SIZE,
        image : 'more.png',
        bottom : '5dp',
        right : '10dp',
        touchEnabled : false
    });

} else {
    moreImage = Ti.UI.createImageView({
        width : Ti.UI.SIZE,
        height : Ti.UI.SIZE,
        image : '/images/more.png',
        bottom : '5dp',
        left : moreImageLeft - 10,
        touchEnabled : false
    });

}
resultView.add(locationImage);
resultView.add(title);
resultView.add(description);
resultViewOuter.add(resultView);
resultViewOuter.add(moreImage);
allViews.push(resultViewOuter);

resultView.addEventListener('singletap', function(rView) {
    var pressedTag = rView.source.tag;
    var pressedTid = rView.source.tid;
    // console.log("Ti.App.adShown = "+Ti.App.adShown);
    if (Ti.App.adShown) {
        addOverlay();
        Ti.App.fireEvent('toMap', {
            result : pressedTid,
            resultTitle : resultsArray[pressedTag].headline
        });
    } else {
        Ti.App.adShown = true;
        Ti.App.fireEvent('toAd', {
            result : pressedTid,
            resultTitle : resultsArray[pressedTag].headline
        });

    }

});

} 但是这个 for 循环没有执行,因为 resultsArray.length 为零,我认为错误是由于将空数组传递给可 ScrollView

最佳答案

这是最新 SDK 中的回归错误 bug report .

我的解决方法是在构造函数中提供一些默认 View 。

scrollableView = Ti.UI.createScrollableView({
    id:"mainMenu",
    scrollType:"horizontal",
    height: "100%",
    showPagingControl: true,
    backgroundColor: "green",
    top: "0",
    width: "100%",
    views: [Ti.UI.createView({})]
});

关于android - 可 ScrollView 应用程序崩溃的 Titanium Android 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669082/

相关文章:

android - 错误 : String types not allowed (at 'src' with value 'splash/splash1.jpg'

ios - UIGestureRecognizer.State 是 'possible' 而不是 'recognized' 每隔一次点击 MKMapView

ios - 如何仅使用一个单元格在一个 View 上设置 UITableView 单元格宽度不同于在 objc 中选择段按钮

javascript - 使用谷歌地理定位坐标数组和从开始到结束的持续时间来计算步速

c# - System.Device.Location.GeoCooperativeWatcher.Position.Location.Speed 始终为 NaN

java - 在静态布局 View 上实现 OnDraw 方法

java - 将十六进制答案转换为 Dec?

android - 在应用程序退出时保存值

ios - 使用 Swift 在 iOS 中将项目添加到数组

python - 从 GeocoderResponse 中提取坐标