我有一个方法可以将片段中的文本字段重置为" &#34 ;.当一切都在MainActivity中时,这很好用。问题是((EditText)v.findViewById(R.id.edAreaxml)).setText("");
。
片段代码是:
public class DataEdit extends Fragment implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dataedit, container, false);
Button btnResetForm = (Button) v.findViewById(R.id.btnResetFormxml);
btnResetForm.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnResetFormxml:
resetForm(v);
break;
}
}
public void resetForm(View v) {
((EditText) v.findViewById(R.id.edAreaxml)).setText(""); //THIS IS THE PROBLEM
}
XML:
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Area">
</TextView>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edAreaxml">
</EditText>
</TableRow>
更新:根据答案1和3,进行了以下调整。还有一个问题。 LogCat输出位于底部。
public class DataEdit extends Fragment implements OnClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dataedit, container, false);
Button btnResetForm = (Button)v.findViewById(R.id.btnResetFormxml);
btnResetForm.setOnClickListener(this);
//EditText edArea=(EditText)v.findViewById(R.id.edAreaxml);
return v;
}@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnResetFormxml:
resetForm(v);
break;
}
}
public void resetForm(View v) {
EditText edArea=(EditText)v.findViewById(R.id.edAreaxml);
edArea.setText(" ");//PROBLEM IS HERE
}
}
Logcat是:
03-21 09:35:20.735: E/Trace(1013): error opening trace file: No such file or directory (2)
03-21 09:35:21.795: D/gralloc_goldfish(1013): Emulator without GPU emulation detected.
03-21 09:35:22.425: I/Choreographer(1013): Skipped 48 frames! The application may be doing too much work on its main thread.
03-21 09:35:25.805: I/Choreographer(1013): Skipped 71 frames! The application may be doing too much work on its main thread.
03-21 09:35:28.254: D/AndroidRuntime(1013): Shutting down VM
03-21 09:35:28.254: W/dalvikvm(1013): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-21 09:35:28.294: E/AndroidRuntime(1013): FATAL EXCEPTION: main
03-21 09:35:28.294: E/AndroidRuntime(1013): java.lang.NullPointerException
03-21 09:35:28.294: E/AndroidRuntime(1013): at net.JonesInc.streamwalker.DataEdit.resetForm(DataEdit.java:139)
03-21 09:35:28.294: E/AndroidRuntime(1013): at net.JonesInc.streamwalker.DataEdit.onClick(DataEdit.java:74)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.view.View.performClick(View.java:4084)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.view.View$PerformClick.run(View.java:16966)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.os.Handler.handleCallback(Handler.java:615)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.os.Looper.loop(Looper.java:137)
03-21 09:35:28.294: E/AndroidRuntime(1013): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-21 09:35:28.294: E/AndroidRuntime(1013): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 09:35:28.294: E/AndroidRuntime(1013): at java.lang.reflect.Method.invoke(Method.java:511)
03-21 09:35:28.294: E/AndroidRuntime(1013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-21 09:35:28.294: E/AndroidRuntime(1013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 09:35:28.294: E/AndroidRuntime(1013): at dalvik.system.NativeStart.main(Native Method)
03-21 09:35:32.204: I/Process(1013): Sending signal. PID: 1013 SIG: 9
答案 0 :(得分:1)
尝试将本地变量初始化为:
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dataedit, container, false);
Button btnResetForm = (Button) v.findViewById(R.id.btnResetFormxml);
btnResetForm.setOnClickListener(this);
editText = (EditText) v.findViewById(R.id.edArea);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnResetFormxml:
resetForm();
break;
}
}
public void resetForm() {
editText.setText("");
}
这样,您的resetForm
方法就不需要View参数。
答案 1 :(得分:0)
初始化EditText
,如下所示,假设它属于dataedit.xml
。
EditText ed;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dataedit, container, false);
ed =(EditText)v.findViewById(R.id.edArea);
Button btnResetForm = (Button) v.findViewById(R.id.btnResetFormxml);
btnResetForm.setOnClickListener(this);
return v;
}
然后
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnResetFormxml:
resetForm(); // no need to pass v as a param
// you could just have ed.setText(""); here itself. no need for resetForm
break;
}
}
然后
public void resetForm() {
ed.setText("");
}
答案 2 :(得分:0)
问题是
public void onClick(View v)
其中v是按钮。
试着把
Button btnResetForm = (Button) v.findViewById(R.id.btnResetFormxml);
et = (EditText)v.findViewById(R.id.edArea);
并定义
EditText et;
作为片段的属性。 之后你可以使用
public void resetForm(View v) {
et.setText("");
}