Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java findViewById在onCreate方法中给出NullPointer异常_Java_Android_Android Layout_Oncreate_Findviewbyid - Fatal编程技术网

Java findViewById在onCreate方法中给出NullPointer异常

Java findViewById在onCreate方法中给出NullPointer异常,java,android,android-layout,oncreate,findviewbyid,Java,Android,Android Layout,Oncreate,Findviewbyid,在使用findViewbyId方法之前,我负责放置setContentView,但它仍然给了我错误。谁能解释一下为什么会发生这种情况。这是我的密码 public class DispActivity extends ActionBarActivity { public String message; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta

在使用findViewbyId方法之前,我负责放置setContentView,但它仍然给了我错误。谁能解释一下为什么会发生这种情况。这是我的密码

public class DispActivity extends ActionBarActivity {

public String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disp);

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

    Intent intent = getIntent();
    message = intent.getStringExtra(MainActivity.Extra);
    EditText txt = (EditText)findViewById(R.id.edit_text23);
    System.out.println("check point 1");
    txt.setText("Having problem here");
    System.out.println("check point 2");


}
fragment_disp.xml看起来像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wifi.DispActivity$PlaceholderFragment" >

<EditText 
    android:id="@+id/edit_text23"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
<Button android:id="@+id/but"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click"/>
</LinearLayout>

您试图将TextView强制转换为EditText,这导致了错误。你确定你已经给出了完整的相关代码吗?堆栈跟踪非常清晰,带有行号。

此代码应放在片段
占位符片段中

EditText txt = (EditText)findViewById(R.id.edit_text23);
System.out.println("check point 1");
txt.setText("Having problem here");
System.out.println("check point 2");
EditText
位于
fragment\u disp.xml
中,您正在布局
R.layout.activity\u disp
中引用该文本,这就是您获得
NPE
的原因。
活动\u disp.xml
没有
EditText

已编辑

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_disp, container, false);

    EditText txt = (EditText) view.findViewById(R.id.edit_text23);
    System.out.println("check point 1");
    txt.setText("Having problem here");
    System.out.println("check point 2");
}

是的,您认为在activity_disp.xml中注册的视图对象可以在onCreate方法中使用是正确的。我试过了,效果很好。但是现在我想知道什么时候在fragment_disp.xml中注册了视图对象,如果两个xml都在做相同的工作(处理布局),为什么我们有两个xml?我试图将我的代码放在Placeholder Fragment类的onCreateView方法中,但我在那里也遇到了同样的错误。@Mercurial请查看我编辑过的答案。你把你的布局像这样膨胀了吗?上述代码将起作用<代码>片段
是显示布局的标准方式,因为它具有灵活性和可重用性。根据Android开发者的说法,即使你在一个活动中只有一个片段,这样做还是比把布局放在活动中更好的做法。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_disp, container, false);

    EditText txt = (EditText) view.findViewById(R.id.edit_text23);
    System.out.println("check point 1");
    txt.setText("Having problem here");
    System.out.println("check point 2");
}