Android onPostExecute没有更新主UI ArrayWheelAdapter WheelView

时间:2013-10-08 01:33:49

标签: android android-asynctask android-arrayadapter

我目前正在尝试从我的AsyncTask内部类中的onPostExecute方法中填充我的WheelView。

下面的代码就像直接放入onCreate()方法一样,但是当在onPostExecute()方法内部时,屏幕上的轮子什么都不显示。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_car_reg);
    ctx = getApplicationContext();

    // text1 = (TextView) this.findViewById(R.id.r1);
    wheel = (WheelView) findViewById(R.id.p1);
    CarAsync carAsync = new CarAsync(this);
    carAsync.execute();

String[] sss = new String[]{"Hi"};
wheel.setViewAdapter(new ArrayWheelAdapter<String>(ctx, sss));
wheel.setVisibleItems(2);
wheel.setCurrentItem(0);
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);

}

这是onPostExecute()方法:

@Override
protected void onPostExecute(ArrayList<Car> cars) {
   String[] sss = new String[]{"Hi"};
   wheel.setViewAdapter(new ArrayWheelAdapter<String>(ctx, sss));
   wheel.setVisibleItems(2);
   wheel.setCurrentItem(0);
   wheel.addChangingListener(changedListener);
   wheel.addScrollingListener(scrolledListener);
   Toast.makeText(getApplicationContext(), "Hitting!", Toast.LENGTH_LONG).show();
}

这是布局XML

<kankan.wheel.widget.WheelView
android:id="@+id/p1"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

wheel 被声明为私有类级别字段。我知道正在达到onPostExecute方法。我设置了断点并插入了吐司。

非常感谢任何帮助!

参考:http://w2davids.wordpress.com/2010/09/28/advanced-android-ui-wheelpicker/

记录错误:
错误

Mon Oct 07 18:29:39 PDT 2013
kankan.wheel.widget.WheelView failed to instantiate.

java.lang.NoClassDefFoundError: Could not initialize class android.view.GestureDetector
    at kankan.wheel.widget.WheelScroller.<init>(WheelScroller.java:86)
    at kankan.wheel.widget.WheelView.initData(WheelView.java:131)
    at kankan.wheel.widget.WheelView.<init>(WheelView.java:115)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:385)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:440)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegateActivated(LayoutEditorDelegate.java:743)
    at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.activated(CommonXmlEditor.java:416)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.LayoutWindowCoordinator.partActivated(LayoutWindowCoordinator.java:379)
    at org.eclipse.ui.internal.PartService$6.run(PartService.java:131)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.ui.internal.PartService.partActivated(PartService.java:129)
    at org.eclipse.ui.internal.WorkbenchPage$12.run(WorkbenchPage.java:4462)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.ui.internal.WorkbenchPage.firePartActivated(WorkbenchPage.java:4460)
    at org.eclipse.ui.internal.WorkbenchPage.access$16(WorkbenchPage.java:4440)
    at org.eclipse.ui.internal.WorkbenchPage$E4PartListener.partActivated(WorkbenchPage.java:173)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl$2.run(PartServiceImpl.java:193)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.firePartActivated(PartServiceImpl.java:191)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:596)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:549)
    at org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer.activate(AbstractPartRenderer.java:105)
    at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer$ActivationJob.run(StackRenderer.java:214)
    at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3946)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3623)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)

2 个答案:

答案 0 :(得分:1)

好吧,我花了几分钟才得到一些工作。我刚编辑了提供的wheel-demo项目的CitiesAvtivity.java类,以使用定义为异步任务的内部类。这很有效。

package kankan.wheel.demo;

import java.util.ArrayList;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;

public class CitiesActivity extends Activity {

ArrayList<String> cities; 
WheelView city;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.cities_layout);
    cities = new ArrayList<String>();
    cities.add("New York");
    cities.add("Washington");
    cities.add("Chicago");
    cities.add("Atlanta");
    cities.add("Orlando");

    city = (WheelView) findViewById(R.id.city);
    city.setVisibleItems(5);
    TestTask task = new TestTask(this);
    task.execute(null);
}

/**
 * Updates the city wheel
 */
private void updateCities(String cities[]) {
    ArrayWheelAdapter<String> adapter =
        new ArrayWheelAdapter<String>(this, cities);
    adapter.setTextSize(18);
    city.setViewAdapter(adapter);
    city.setCurrentItem(cities.length / 2);        
}


private class TestTask extends AsyncTask<Void, Void, ArrayList<String>> {
    private ProgressDialog wait;

    public TestTask(Context context){
        wait = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {

        wait.show();

    }
    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        try {
            Thread.sleep(3000); //This is where you actually do what you need to
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return cities;
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        String[] array = new String[result.size()];
        result.toArray(array);
        updateCities(array);
        wait.dismiss();
    }
}

}

答案 1 :(得分:0)

堆栈跟踪是关于Eclipse而不是Android。 Android中发生的事情是,您的onPostExecute()永远不会被调用。此方法是AsyncTask的一部分,之前是doInBackground()的运行。

如果您输入onPostExecute()并完成doInBackground(),请查看。