【问题标题】:Action Bar for Android 2.2, Handler not updating by UIAndroid 2.2 的操作栏,处理程序未通过 UI 更新
【发布时间】:2012-07-23 16:41:57
【问题描述】:

我正在尝试抽象并创建一个操作栏(适用于 Android 2.2)。因此我自己写出来。由于某种原因,我无法让 HomeScreen 更新操作栏。这可能不是处理程序问题,比我猜测的更基本。

我的类图就像 IBar(接口)->implementedby-> ActionBarActivity(抽象类)->extendedby-> HomeScreen 使用类God公共静态方法。

Only relevant pieces of code below

ActionBarActivity,被我所有的 Activity 使用。

public abstract class ActionBarActivity extends Activity implements IBar {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionbar);

    connectionStatus = (ImageView) findViewById(R.id.connectStatus);
    connectionStatus.setOnClickListener(this);
    if (God.isConnectedToServer)
        connectionStatus.setImageDrawable(this.getResources().getDrawable(
                R.drawable.connect));
    else
        connectionStatus.setImageDrawable(this.getResources().getDrawable(
                R.drawable.disconnect));
    connectionStatus.setEnabled(true);

public void setConnectionStatus(boolean status) {
    Bundle bundle = new Bundle() ;
    bundle.putSerializable(God.BARVALUES, (IBar) this) ;
    Message message = updateBar.obtainMessage() ;
    message.setData(bundle);
    message.sendToTarget();
}
@Override
public boolean handleMessage(Message barData) {
    IBar barValues = (IBar) barData.getData()
            .getSerializable(God.BARVALUES);
--->>>if (barValues.getConnectionStatus())
        connectionStatus.setImageDrawable(this.getResources().getDrawable(
                R.drawable.connect));
    else if (!barValues.getConnectionStatus())
        connectionStatus.setImageDrawable(this.getResources().getDrawable(
                R.drawable.disconnect));
    return false;
}
public abstract boolean getConnectionStatus();

HomeScreen 扩展了 ActionBarActivity

public class HomeScreen extends ActionBarActivity {
@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    try {
        God.switchServer((ActionBarActivity)this, God.mCruiseOnServer);
    } catch (InvalidServerDNSorIPException e) {
        Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
        return ;
    }

神级,公共静态。

public class God {
public static boolean switchServer(ActionBarActivity activity, String newServer) {
    ...
    activity.setConnectionStatus(true) ;
    ....
}

操作栏上的图片按钮

<ImageView
    android:id="@+id/connectStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/refresh"
    android:contentDescription="@string/connectionStatus" />

编辑:我在调试器下运行了这个,我正在执行已经达到 --->>> 行。这意味着我的 connection.setImageDrawable 被调用,但由于某种原因没有显示在我的 UI 上。我在这里遗漏了一些非常基本的东西。

【问题讨论】:

  • 我没有操作栏实现。
  • :) 我以为我在什么地方看到了它。抱歉,我会更新它。
  • 您有什么理由决定自己设计与使用 ActionBarSherlock 之类的东西?
  • 不,没有参考指南,需要阅读他们所有的代码来弄清楚如何使用/自定义它。一开始就很痛苦。
  • 放弃它,使用需要弄清楚的框架太痛苦了。不管它有多好。

标签: android android-handler


【解决方案1】:

根本原因是基类有一个 R.layout.actionbar,它与 R.id.actionbar 的实例不同,而不是我的主屏幕。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2016-05-17
    • 2018-11-16
    • 2013-08-02
    • 2014-02-21
    相关资源
    最近更新 更多