javascript - Titanium Android 应用程序在更新 ScrollableView 时崩溃

标签 javascript android titanium

首先:一切都按照 iOS 的预期运行。

我认为问题是由 titan 生成的某些 Java 对象没有获取更新可 ScrollView 大小的参数。

错误日志:

[WARN] :   ResourceType: No known package when getting name for resource     number 0xffffffff
[WARN] :   dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d57b20)
[ERROR] :  TiApplication: (main) [674,5584] Sending event: exception on thread: main msg:java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 0 Pager id: ffffffff Pager class: class ti.modules.titanium.ui.widget.TiUIScrollableView$1 Problematic adapter: class ti.modules.titanium.ui.widget.TiUIScrollableView$ViewPagerAdapter; Titanium 3.5.0,2015/01/12 15:33,0014f83
[ERROR] :  TiApplication: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 0 Pager id: ffffffff Pager class: class ti.modules.titanium.ui.widget.TiUIScrollableView$1 Problematic adapter: class ti.modules.titanium.ui.widget.TiUIScrollableView$ViewPagerAdapter
[ERROR] :  TiApplication:   at android.support.v4.view.ViewPager.populate(ViewPager.java:962)
[ERROR] :  TiApplication:   at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
[ERROR] :  TiApplication:   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.constrainChild(TiCompositeLayout.java:398)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.onMeasure(TiCompositeLayout.java:278)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.constrainChild(TiCompositeLayout.java:398)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.onMeasure(TiCompositeLayout.java:278)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.constrainChild(TiCompositeLayout.java:398)
[ERROR] :  TiApplication:   at org.appcelerator.titanium.view.TiCompositeLayout.onMeasure(TiCompositeLayout.java:278)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
[ERROR] :  TiApplication:   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
[ERROR] :  TiApplication:   at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
[ERROR] :  TiApplication:   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
[ERROR] :  TiApplication:   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
[ERROR] :  TiApplication:   at android.view.View.measure(View.java:16497)
[ERROR] :  TiApplication:   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
[ERROR] :  TiApplication:   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
[ERROR] :  TiApplication:   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
[ERROR] :  TiApplication:   at   android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
[ERROR] :  TiApplication:   at  android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
[ERROR] :  TiApplication:   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
[ERROR] :  TiApplication:   at android.view.Choreographer.doCallbacks(Choreographer.java:574)
[ERROR] :  TiApplication:   at android.view.Choreographer.doFrame(Choreographer.java:544)
[ERROR] :  TiApplication:   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
[ERROR] :  TiApplication:   at android.os.Handler.handleCallback(Handler.java:733)
[ERROR] :  TiApplication:   at android.os.Handler.dispatchMessage(Handler.java:95)
[ERROR] :  TiApplication:   at android.os.Looper.loop(Looper.java:136)
[ERROR] :  TiApplication:   at android.app.ActivityThread.main(ActivityThread.java:5001)
[ERROR] :  TiApplication:   at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR] :  TiApplication:   at java.lang.reflect.Method.invoke(Method.java:515)
[ERROR] :  TiApplication:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
[ERROR] :  TiApplication:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
[ERROR] :  TiApplication:   at dalvik.system.NativeStart.main(Native Method)
[INFO] :   Process: Sending signal. PID: 1280 SIG: 9

直接设置可 ScrollView 的 .views 属性或使用 .setViews() 时会发生崩溃。

相关代码:

//on scrolling of the Years view fill the makes and model views
function populateMake(e){   
$.makeScrollableView.scrollToView(0);
removeAllChildren($.makeScrollableView);

//Generating URL        
var selectedYear = $.yearScrollableView.currentPage; //selected index
var carYearViews = $.yearScrollableView.getViews(); //array of views
var children = carYearViews[selectedYear].getChildren(); //children of the selected year (aka the label w/ the year in it)
var makeYear = children[0].getText();

var url = "www.example.com"; 
var makeViews = [];
var client = Ti.Network.createHTTPClient({
    onload : function(e){
        var yearMakes = [];
        yearMakes = parseMakes(this.responseText); //returns an array of strings

        for(var i = 0; i < yearMakes.length; i++){
            var yearLabel = Ti.UI.createLabel({
                title: "year",
                text: yearMakes[i]
            });
            var view = Ti.UI.createView({
                backgroundColor: 'blue'
            });
            view.add(yearLabel);
            makeViews.push(view);
        }
        $.makeScrollableView.setViews(makeViews); //<--- crashes here when scrolling $.yearScrollableView

        populateModel();
    },
    onerror : function (e){
        Ti.API.error("HTTP Error populating makes");
    }
});
client.open("GET", url);
client.send(); 
}

并且:

//populate model when year/make is changed
function populateModel(e){
$.modelScrollableView.scrollToView(0);
removeAllChildren($.modelScrollableView);

var selectedYear = $.yearScrollableView.currentPage; //selected index

var carYearViews = $.yearScrollableView.getViews(); //array of views
var childrenYear = carYearViews[selectedYear].getChildren(); //children of the selected year (aka the label w/ the year in it)
var carYear = childrenYear[0].getText();

var url = "www.totallyafakewebsite.net";
Ti.API.info(url);
var modelViews = [];
var client = Ti.Network.createHTTPClient({
    onload : function(e){
        var carModels = [];
        carModels = parseModels(this.responseText); //array of strings

        for(var i = 0; i < carModels.length; i++){
            var modelLabel = Ti.UI.createLabel({
                title: "model",
                text: carModels[i]
            });
            var view = Ti.UI.createView({
                backgroundColor: 'blue'
            });
            view.add(modelLabel);
            modelViews.push(view);
        }


        $.modelScrollableView.setViews(modelViews); //<--- this is the crash when scrolling $.makeScrollableView
        //$.modelScrollableView.views = modelViews;   //doesn't work either
    },
    onerror : function (e){
        Ti.API.error("HTTP Error populating models: " + e.error);
    }
});
client.open("GET", url);
client.send();
}

当相应的可 ScrollView 滚动时,会触发这些事件。

每个可 ScrollView 都是在 xml View 中创建的。

最佳答案

我发现了自己的问题:

发生这种情况的原因是我自己的removeAllChildren函数不起作用。因为钛的功能不起作用,所以我必须创建自己的功能。

函数的内部看起来像这样:

$.scrollableView.values = []; //an empty array

我使用了以下代码:

//Remove old data from $.makeScrollableView
$.makeScrollableView.setCurrentPage(0);
makeViews.length = 0;

将当前索引设置为 0,然后将数组长度设置为 0 解决了我的问题。

关于javascript - Titanium Android 应用程序在更新 ScrollableView 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857179/

相关文章:

javascript - onclick选择子元素

javascript - 文本输入中的打字机效果在 Firefox 中不起作用

android - 使用 Android SDK 4.1 版与 2.3 版编译的优势?

android - 如何在表格行上实现长按/滑动和删除 - android titanium?

android - 应用内购买 Titanium Android

javascript - 为什么 this.evaluate 不能正确返回 DOM 节点?

javascript - 涉及异步调用时,如何设置具体的执行顺序?

android - 在 Android 4.0.3 中以编程方式访问 WiFi 设置

java - _ID不存在

android - 跨平台推送消息 - Native 还是 Phonegap/Titanium?