EditTexts を使用する代わりに、ユーザーの入力を受け入れるダイアログを表示するクリック可能なカテゴリを使用することにしました。この再利用可能な Dialog クラスは、AllMethods.class に格納されています。最初の理論は、一連のテキスト (説明、タイトルなど) とターゲット TextView (ユーザーがダイアログに入力した setText) を渡すというものでした。ただし、実行時に TextView.setText(str) コード行を実行しようとすると、nullpointer 例外が発生します。
だから、私の質問:ユーザーが [OK] ボタンをクリックした後にのみ、textView を正常に変更するか、文字列を戻すにはどうすればよいですか?
また、TextView が宣言され、インスタンス化されていることにも注意してください。nullpointer は、リモート クラスにあるためだと思います。コードは次のとおりです。
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class AllMethods {
//fieldRequest is a reusable prompt to get a user's input for a field without taking up so much space
/* Legend of inputs:
* requestingActivity=the activity which is prompting the popup
* dialogMessage = the description of the editText being requested
* dialogTitle = the title to be displayed on the dialogbox
* editTextHint = the Hint attribute that will give the user an example of the expected input
* inputType = numeric, text, phone number, etc
*/
public static void fieldRequest(Activity activity, String dialogMessage, String dialogTitle, final TextView outputText, int inputType, String optionalFieldSuffix){
final Dialog dialog= new Dialog(activity);
dialog.setContentView(R.layout.dialog_layout);
dialog.setTitle(dialogTitle);
//If the message/description exists, put it in
if(dialogMessage!=null||dialogMessage!=""){TextView description = (TextView) dialog.findViewById(R.id.dialog_description);
description.setText(dialogMessage);
}
//Identify the editText, set the input type appropriately and fill in the hint if applicable
final EditText inputField = (EditText) dialog.findViewById(R.id.dialog_inputbox);
if(Integer.toString(inputType)!=null){inputField.setInputType(inputType);}
if(optionalFieldSuffix!=null){TextView suffix = (TextView) dialog.findViewById(R.id.dialog_optionalFieldSuffix);
suffix.setText(optionalFieldSuffix);}
ImageButton dialogCancel = (ImageButton) dialog.findViewById(R.id.dialog_cancel);
ImageButton dialogDone = (ImageButton) dialog.findViewById(R.id.dialog_done);
dialogCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogDone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
outputText.setText(inputField.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}