【问题标题】:Calling button from another Activity从另一个 Activity 调用按钮
【发布时间】:2012-05-21 12:12:22
【问题描述】:

如何从另一个 Activity 编辑按钮?

它返回给我一个

java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.NullPointerException

当我想要时

getButton.setEnabled(false);

在哪里

Button getButton = (Button)findViewById(R.id.buttonGet);

【问题讨论】:

    标签: android button android-activity


    【解决方案1】:

    我认为你不能,或者至少它不是“Android 方式(tm)”。
    相反,要么设置一个首选项标志并在目标活动的 OnResume 上读取它,或者如果目标(带有按钮的那个)调用另一个,你可以使用 setResult(...) 返回一些东西并在 onActivityResult(.. .).

    编辑:看起来我回答了他所说的“从另一个调用一个按钮(即与当前不同)”活动,但他实际上 的意思是他在当前一个中找不到他的按钮: )

    【讨论】:

      【解决方案2】:

      确保在获得对按钮的引用之前调用了 setContentView,即这样做:

      setContentView(R.layout.my_activity_layout);
      

      在此之前:

      Button getButton = (Button)findViewById(R.id.buttonGet);
      

      【讨论】:

      • 那么他所说的“另一项活动”是什么意思? :)
      • 我认为 Coretek 的意思是一个 xml 布局文件。
      猜你喜欢
      • 2016-05-26
      • 2020-02-20
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      相关资源
      最近更新 更多