EditText条目上的Android Null对象参考[重复]
Posted
技术标签:
【中文标题】EditText条目上的Android Null对象参考[重复]【英文标题】:Android Null Object Reference on EditText entry [duplicate] 【发布时间】:2018-01-30 20:54:13 【问题描述】:已经有很多帖子寻求有关此错误的帮助,但我找不到可以解释我的情况的帖子。我的应用程序的主屏幕要求用户输入与游戏结束后的日期/分数相对应的用户名。显然,游戏丢失的分数/日期(时间)直到最后都是未知的,所以只有用户名被添加到前端的数据库中,但由于某种原因,我的代码在应用程序到达之前就生成了一个错误请求信息的重点。
尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText()' 在空对象引用上
代码如下。我对 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_
android:layout_
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:text="Username"
android:id="@+id/textViewName"/>
<EditText
android:layout_
android:layout_
android:id="@+id/editTextName"/>
<Button
android:layout_
android:layout_
style="?android:attr/buttonStyleSmall"
android:text="Add"
android:id="@+id/btnAdd"/>
<Button
android:layout_
android:layout_
android:text="Last 5 Games"
android:id="@+id/btnLastFive"/>
<RelativeLayout
android:layout_
android:layout_ >
<TextView
android:id="@+id/text"
android:text="@string/snake_layout_text_text"
android:visibility="visible"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
</LinearLayout>
我不认为这是与现有帖子(我确实阅读过)的重复,因为之前的示例都没有涉及来自用户(我的 Edittext)的输入,或者包含错误引用的代码行没有存在于我的代码中(EditText.getText()) 也许是我的经验不足导致之前的答案无法解释导致此错误的原因,但另一个问题对我没有帮助。如果这真的是一个骗局,给您带来的不便深表歉意,那不是我的意图。
【问题讨论】:
你的错误是空对象上的edittext.gettext,在你的代码中它只是edittext.tostring()..请发布正确的代码以及你分配edittext id的xml代码 这是问题的一部分,Aalap,我已经搜索了整个项目,但找不到使用 edittext.gettext 的实例。 onClick(v View) 方法看起来有点神秘,不应该封装在 onClickListener 中吗? 这很有帮助,@ScottishUser 并帮助解决了问题。如果可以的话,我会感谢你指出我这一点。 【参考方案1】:尝试使用 editTextName.getText.toString() 代替 editTextName.toString()
【讨论】:
以上是关于EditText条目上的Android Null对象参考[重复]的主要内容,如果未能解决你的问题,请参考以下文章