【问题标题】:How to call findViewById(R.id.textView) in another class than MainActivity?如何在 MainActivity 之外的另一个类中调用 findViewById(R.id.textView)?
【发布时间】:2020-10-07 05:59:40
【问题描述】:

我是 Android 新手。 我正在尝试从一个单独的类而不是 MainActivity 引用按 Id 的视图。

注意:我的应用只有一个 Activity。

主要活动:

public class MainActivity extends AppCompatActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState)//Activity Oncreate callback
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) //Oncreate Options_menu callback
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) {
    case R.id.option1:
    //Here am calling a method from another class

    SecondClass secondClassObject=new SecondClass();
    secondClassObject.method1();
    }
    return true;
}

二等:

public class SecondClass {
    public void method1(){
        TextView tv1 = (TextView)findViewById(R.id.textView);
        tv1.setText("");
    }
}

SecondClass中如何通过ID引用textView?如何在此 SecondClass 中将上下文设置为 MainActivity?

【问题讨论】:

  • 第二个类不仅仅是另一个java类。它扩展了 AppCompatActivity,你不应该自己初始化对象,android 系统会为你做。您应该使用一个按钮来打开另一个活动。 TextView tv1 = (TextView)findViewById(R.id.textView) 这会引发错误,因为尚未创建布局且视图不存在。
  • 好的,现在我更新了问题及其解释。你能建议我怎么做吗?
  • 将 (Context)MainActivity 作为参数传递给构造函数或方法。
  • @PeterMmm 你能把它作为答案吗?
  • 在外部对象尤其是全局对象中保存活动时要小心。这将导致内存泄漏。

标签: java android android-activity android-context


【解决方案1】:

为您的方法添加一个 Activity 参数

public void method1(Activity act){
    TextView tv1 = (TextView)act.findViewById(R.id.textView);
    tv1.setText("");
}

从你的活动中定义一个活动变量Activity act;,以便在其他函数中使用它。将你的活动的值分配给变量act=this;,最后在你想要的函数上使用它。

                public class MainActivity extends AppCompatActivity {

                Activity act;
                 @Override
                    protected void onCreate(Bundle savedInstanceState)//Activity Oncreate callback
                    {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        act=this;
                    }
                @Override
                    public boolean onCreateOptionsMenu(Menu menu) //Oncreate Options_menu callback
                    {
    act=this;

                        // Inflate the menu; this adds items to the action bar if it is present.
                        getMenuInflater().inflate(R.menu.main_menu, menu);
                        return true;
                    }
                    @Override
                    public boolean onOptionsItemSelected(MenuItem item) { 
                    switch(item.getItemId()) {
                    case R.id.option1:
                    //Here am calling a method from another class

                    //SecondClass secondClassObject=new SecondClass();
                    //secondClassObject.method1();
                    SecondClass secondClassObject=new SecondClass();
                                secondClassObject.method1(act);
                    }
                    return true;
                }

你也可以传递父布局视图

  public void method1(View act){
    TextView tv1 = (TextView)act.findViewById(R.id.textView);
    tv1.setText("");
  }

这样称呼它

 SecondClass secondClassObject=new SecondClass();
  secondClassObject.method1(findViewById(R.id.your_parent_layout));

如果不是绝对必要,请避免使用传递 Activity 方法以避免内存泄漏的可能性。

【讨论】:

  • 但现在它在行抛出错误“java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on an null object reference” "tv1.setText("");"
  • 在外部对象尤其是全局对象中保存活动时要小心。这将导致内存泄漏。
  • @SayanSen 那是因为您的 findViewById 返回 null。我建议你运行调试器或添加一些日志,看看有什么问题
  • 检查活动的 XML 以确保存在 id 为“textView”的 textView
  • 但我无法遵循这个答案,因为它可能会导致内存泄漏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 2016-01-23
  • 2016-02-28
相关资源
最近更新 更多