TextView.setText()位置混淆:onCreate()vs userMethod()

时间:2014-06-29 12:16:28

标签: android textview oncreate settext

我很好奇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

有什么区别?

2 个答案:

答案 0 :(得分:2)

TextView显然是片段布局。您试图在填充片段之前到达textview,这就是为什么会出现空指针异常的原因。

尝试在fragment 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;
    }

答案 1 :(得分:0)

实际上TextView位于fragment_main.xml,您在将片段添加到setText之前调用了mainActivity,因为没有textview,所以它会生成NullpointerException你的activity_main.xml