【发布时间】:2014-04-30 22:46:15
【问题描述】:
所以这就是问题所在。我正在编写一个应用程序,其中一个片段和一个按钮中有两个 EditText 视图。单击按钮时,它会更改 EditText 的文本。在 onViewCreated() 方法中,我使用此代码获取 EditText 实例并将其存储到变量中。
EditText box1 = (EditText)(getView.findViewById(R.id.box1));
这很好用。但是,当我在单击按钮时尝试访问变量 box1 时,EditText 变为 null 并引发 NullPointerException。有谁知道为什么会这样?
这里是片段代码:
public class MyFragment extends Fragment {
EditText box1, box2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_text_coder_free,
container, false);
//Works here.
box1 = (EditText)(rootView.findViewById(R.id.box1));
box2 = (EditText)(rootView.findViewById(R.id.box2));
//Same thing. Sets the text correctly
box1.setText("hey look at me!");
return rootView;
}
//Called when the button is clicked.
public void setBox1Text(String text) {
//It's null here so it skips to the else
if(box1 != null) {
box1.setText(text);
}
else {
//This doesn't work. Throws the exception here.
box1 = (EditText)(getView().findViewById(R.id.box1));
}
}
public void setBox2Text(String text) {
if(box2 != null) {
box2.setText(text);
}
else {
//This doesn't work. Throws the exception here.
box2 = (EditText)(getView().findViewById(R.id.box2));
}
}
public String getBox1Text() {
if(box1 == null) {
return "Nice try.";
}
return box1.getText().toString();
}
public String getBox2Text() {
if(box2 == null) {
return "Nice try.";
}
return box2.getText().toString();
}
}
这是存放片段的活动:
public class MyActivity extends Activity {
MyFragment myFragment
EditText box1, box2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_coder);
if (savedInstanceState == null) {
myFragment = new MyFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, myFragment.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_app, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void box1Click(View v) {
myFragment.setBox2Text("Some text");
}
public void box2Click(View v) {
myFragment.setBox2Text("Some text");
}
}
这是片段中的 XML:
<RelativeLayout 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.mycode.MyFragment" >
<EditText
android:id="@+id/box1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/box2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/box1"
android:inputType="textMultiLine|none"
android:focusable="false" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/box2"
android:layout_alignParentRight="true"
android:onClick="box2Click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/encrypt_button_text"
android:layout_below="@id/box2"
android:layout_toLeftOf="@id/button2"
android:onClick="box1Click" />
</RelativeLayout>
【问题讨论】:
-
发布相关的xml和stacktrace
-
你能发布你的代码吗?
-
是的,对不起。在这里。
-
@Cybran 这个
setBox1Text(String text)叫什么名字?我的意思是点击哪个按钮,按钮在哪里? -
从持有fragment的Activity调用。等等,我也补充一下。
标签: android nullpointerexception fragment