【问题标题】:TextView.setText() location confusion: onCreate() vs userMethod()TextView.setText() 位置混淆:onCreate() vs userMethod()
【发布时间】:2014-06-29 12:16:28
【问题描述】:

我很好奇setText() 实际在哪里工作。这是我的问题的示例代码。我用“...”省略了不相关的代码。

// fragment_main.xml
<RelativeLayout ... >

    <TextView
    android:id="@+id/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="userMethod" />

</RelativeLayout>

// ActivityMain.java
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.hello_world);
    textView.setText("This cause runtime error!!");

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    } // if
} // onCreate

    /**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

                         // gives a compile error about non-static finViewById here
                 // TextView textView = (TextView) findViewById(R.id.hello_world);
                 // textView.setText("Compiling Error!!");


        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    } // onCreateView
} // PlaceholderFragment


// user defined method 
public void userMethod(View view) {
    TextView textView = (TextView) findViewById(R.id.hello_world);
    textView.setText("This run successful");
} // userMethod
} // class MainActivity

这是我不明白的:

textView.setText("string")protected void onCreate(...) 中失败

但是

textView.setText("string")public void userMethod(...) 时运行

这两种方法都在public class MainActivity extends ActionBarActivity

有什么区别??

【问题讨论】:

  • 未使用 userMethod(View view),如果是,则应在 xml 中发布 onClick 标志。
  • 谢谢。我认为包含

标签: android textview oncreate settext


【解决方案1】:

TextView 显然是在片段布局中。你试图在你膨胀片段之前到达文本视图,这就是你得到空指针异常的原因。

尝试在片段 onCreateView() 方法内或片段膨胀后设置文本。

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // we inflate the fragment here.
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
       // get the textview from inflated layout.
       TextView textView = (TextView) rootView.findViewById(R.id.hello_world);
       textView.setText("Should work");

       return rootView;
    }

【讨论】:

  • 谢谢!我了解 C++ 和 PHP 方面的编程......“膨胀”的概念对我来说是全新的......当我将 textView 声明和 setText 放入 onCreateView 时,我收到此错误Cannot make a static reference to the non-static method findViewById(int) from the type Activity MainActivity.java ...当我声明 textView 全局并且 onCreateView 中的 setText 得到 Cannot make a static reference to the non-static field textView MainActivity.java 指的是 onCreateView 中的 textView.setText() ... 我的目标是在不重新创建整个活动的情况下更改对象/视图的值
  • 你能展示一下你是如何在片段中声明 textview 的吗?里面可能有问题。
  • 我在 fragment_main.xml 中提供了完整的 标签
  • 我的意思是片段 java 类。你能复制你的片段 onCreateView() 方法吗?
  • 我推测 MainAvtivity.java 中的 PlaceholderFragment,但它现在在那里
【解决方案2】:

实际上 TextView 在您的 fragment_main.xml 中,并且您在将片段添加到 mainActivity 之前调用 setText 并且它将生成 NullpointerException 因为您的 activity_main.xml 中没有 textview。

【讨论】:

  • 谢谢!为了清楚起见,activity_main.xml 是否首先运行,然后是 fragment_main.xml(它是主要活动中的对象/组视图?)。虽然层次结构是TextView &gt;&gt; Fragment &gt;&gt; MainActivity。 onCreate() 是否仅编译主要活动,因此 TextView 尚不存在,但在调用 userMethod() 时 TextView 已经存在?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
相关资源
最近更新 更多