このエラーの助けを求める投稿がたくさんありましたが、私のシナリオを説明した記事は見つかりませんでした。私は、あなたのアプリケーションのホーム画面を持って、ユーザに、それが終わるとゲームの日付/スコアに対応するユーザ名を入力するように要求します。明らかに、ゲームが失われるスコア/日付(時間)は、最後までわからないので、フロントエンドのDBにユーザー名だけが追加されていますが、何らかの理由で私のコードがアプリケーションに到達する前にエラーを生成しています情報を要求する地点。EditTextエントリーのAndroid Nullオブジェクトリファレンス
仮想メソッドを呼び出す試み 'android.text.Editable android.widget.EditText.getText()' NULLオブジェクト参照に
コードは以下です。私はまだAndroidの方が新しいので、コードが間違った場所にあるか間違った時刻に呼び出されたかどうかはわかりません。
これはエラーを投げているようです。
protected void insertIntoDB(){
ContentValues cv = new ContentValues();
cv.put (db.USERNAME, editTextName.toString());
}
これは、残りのクラスとの文脈での行です。
public class Snake extends AppCompatActivity {
DBHandler db;
Calendar c = Calendar.getInstance();
Date d = c.getTime();
private SnakeView mSnakeView;
private static String ICICLE_KEY = "snake-view";
private static Context mContext;
private EditText editTextName;
private Button btnAdd;
private Button btnLastFive;
private RelativeLayout mRelativeLayout;
private PopupWindow mPopupWindow;
/**
* Called when Activity is first created. Turns off the title bar, sets up
* the content views, and fires up the SnakeView.
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
mContext = getApplicationContext ();
//set the layout
setContentView (R.layout.snake_layout_r);
db = new DBHandler (this);
editTextName = (EditText) findViewById (R.id.editTextName);
btnAdd = (Button) findViewById (R.id.btnAdd);
btnLastFive = (Button) findViewById (R.id.btnLastFive);
mSnakeView = (SnakeView) findViewById (R.id.snake);
mSnakeView.setTextView ((TextView) findViewById (R.id.text));
SimpleDateFormat dateFormat = new SimpleDateFormat ("MM/dd/yyyy");
String format = dateFormat.format (d);
if (savedInstanceState == null) {
// We were just launched -- set up a new game
mSnakeView.setMode (SnakeView.READY);
} else {
// We are being restored
Bundle map = savedInstanceState.getBundle (ICICLE_KEY);
if (map != null) {
mSnakeView.restoreState (map);
} else {
mSnakeView.setMode (SnakeView.PAUSE);
}
}
}
public static Context getAppContext() {
return mContext;
}
@Override
protected void onPause() {
super.onPause();
// Pause the game along with the activity
mSnakeView.setMode(SnakeView.PAUSE);
}
@Override
public void onSaveInstanceState(Bundle outState) {
//Store the game state
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
}
private Date currentdate(){
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return cal.getTime();
}
protected void insertIntoDB(){
ContentValues cv = new ContentValues();
cv.put (db.USERNAME, editTextName.toString());
}
public void lastFive(String[] args) {
List<User> list = new ArrayList<User> ();
Collections.sort (list, User.Comparators._score);
}
public void onClick(View v) {
if(v == btnAdd){
insertIntoDB();
} else if (v == btnLastFive){
// Initialize a new instance of LayoutInflater service
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflate the custom layout/view
View customView = inflater.inflate(R.layout.show_last_five,null);
/*
public PopupWindow (View contentView, int width, int height)
Create a new non focusable popup window which can display the contentView.
The dimension of the window must be passed to this constructor.
The popup does not provide any background. This should be handled by
the content view.
Parameters
contentView : the popup's content
width : the popup's width
height : the popup's height
*/
// Initialize a new instance of popup window
mPopupWindow = new PopupWindow(
customView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
// Set an elevation value for popup window
// Call requires API level 21
if(Build.VERSION.SDK_INT>=21){
mPopupWindow.setElevation(5.0f);
}
// Get a reference for the custom view close button
ImageButton closeButton = (ImageButton) customView.findViewById(R.id.ib_close);
// Set a click listener for the popup window close button
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Dismiss the popup window
mPopupWindow.dismiss();
}
});
/*
public void showAtLocation (View parent, int gravity, int x, int y)
Display the content view in a popup window at the specified location. If the
popup window cannot fit on screen, it will be clipped.
Learn WindowManager.LayoutParams for more information on how gravity and the x
and y parameters are related. Specifying a gravity of NO_GRAVITY is similar
to specifying Gravity.LEFT | Gravity.TOP.
Parameters
parent : a parent view to get the getWindowToken() token from
gravity : the gravity which controls the placement of the popup window
x : the popup's x location offset
y : the popup's y location offset
*/
// Finally, show the popup window at the center location of root relative layout
mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);
}
};
XMLファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="@+id/textViewName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextName"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/buttonStyleSmall"
android:text="Add"
android:id="@+id/btnAdd"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last 5 Games"
android:id="@+id/btnLastFive"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:text="@string/snake_layout_text_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
</LinearLayout>
私は、ユーザからの入力を関与これは、前のサンプルのどれとして(私が読んでなかった)既存の投稿の複製であることを信じていませんでした(私のEDITTEXT)私のコード(EditText.getText())に存在しなかったコード行をエラーが参照していることが含まれている可能性があります。おそらく、このエラーがスローされる原因を説明していない以前の回答を導く経験不足ですが、私には助けにはならなかった。これが真実であれば不便をお詫びします。それは私の意図ではありません。
あなたのエラーは、nullオブジェクトにし、あなたのコード内のedittext.gettextが言うeditTextName.getText.toString()の代わりに、editTextName.toStringを()してみてちょうど()..も正しいコードを投稿してくださいedittext.tostring xmlコードはどこにuを割り当てましたかedittext id –
これは問題の一部です、Aalap、私はプロジェクト全体を検索し、edittext.gettextを使用したインスタンスが見つかりません。 –
onClick(v View)メソッドは少し不思議そうですが、onClickListener内にカプセル化してはいけませんか? – ScottishUser