【问题标题】:Android Presentation Class - How to make changes on the Presentation View dynamicallyAndroid Presentation Class - 如何动态更改 Presentation View
【发布时间】:2013-07-02 17:54:49
【问题描述】:

我的演示文稿终于奏效了。我的第一个屏幕有一个主activity,第二个屏幕有一个Presentation。 我的问题是,我无法更改演示视图中的内容。

为什么我不能在第二个屏幕上显示演示文稿后更改TextView? 在MainActivity 中调用changeText("Test123") 方法会使我的应用程序崩溃。

public class MainActivity extends Activity {

    private PresentationActivity presentationActivity;

    protected void onCreate(Bundle savedInstanceState) {    

        super.onCreate(savedInstanceState);  


        // init Presentation Class

        DisplayManager displayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
        Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        if (presentationDisplays.length > 0) {
            // If there is more than one suitable presentation display, then we could consider
            // giving the user a choice.  For this example, we simply choose the first display
            // which is the one the system recommends as the preferred presentation display.
            Display display = presentationDisplays[0];
            PresentationActivity presentation = new PresentationActivity(this, display);
            presentation.show();

            this.presentationActivity =  presentation;    
        }
    }

    public void changeText (String s) {

        this.presentationActivity.setText(s);

    }
}



public class PresentationActivity extends Presentation {

    private TextView text;



    private PresentationActivity presentation;


public PresentationActivity(Context outerContext, Display display) {
    super(outerContext, display);
    // TODO Auto-generated constructor stub

}

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

    setContentView(R.layout.activity_presentation);   

    TextView text = (TextView) findViewById(R.id.textView1);

    this.text = text;
    // works fine:
    text.setText("test");      

}

public void setText(String s){

    // error
    this.text.setText(s);

}

【问题讨论】:

  • 使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪。
  • 嗨!我无法使用 LogCat,因为我的 USB 端口用于将第二个屏幕与 HDMI 连接。
  • edit:好的,有一些适用于 LogCat 的应用程序。我明天测试一下。谢谢!
  • 然后在崩溃后断开 MHL(?) 电缆,将其插入开发机器上的 USB,然后然后检查 LogCat。或者,您可以让 adb 通过 USB 工作,请参阅 tech.leolink.net/2012/11/use-adb-wifi-without-rooting-for.html。您引用的应用程序将无法运行,因为您无法在 Android 4.1+ 上的应用程序中查看来自其他进程的 LogCat 消息。

标签: android screen presentation


【解决方案1】:

嗯,我查看了 LogCat。 例外是:

E/AndroidRuntime(13950): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

MainActivity 中的代码在另一个线程上运行。要从这里做 UI 工作,我需要使用 runOnUiThread。我在this 答案中找到了这个解决方案。

我的changeText 方法现在看起来像这样:

public void changeText (String s) {

        runOnUiThread(new Runnable() {
          public void run() {
              presentationActivity.setImageView(position);
          }
    });
}

感谢您的帮助!现在我知道如何使用 LogCat 来做类似的事情了。

【讨论】:

    【解决方案2】:

    您遇到此问题是因为演示的上下文与包含的 Activity 的上下文不同:

    Presentation 在创建时与目标 Display 相关联 并根据 显示的指标。

    值得注意的是,演示文稿的上下文与上下文不同 其包含的活动。膨胀布局很重要 演示文稿并使用演示文稿自己的资源加载其他资源 以确保资产的大小和密度正确 目标显示已加载。

    希望这也能证明您提到的解决方案是合理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 2021-07-14
      • 1970-01-01
      • 2010-11-27
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      相关资源
      最近更新 更多