当我在popupWindow
输入信息时会发生这种情况。
Java代码:
package com.example.istronggyminstructor;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class CurrentUsers extends Activity {
private Button register;
private Button adduser;
EditText getusertext, getpass, getweight, textdisp;
DBAdapter myDB;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_users);
register = (Button)findViewById(R.id.regbut);
adduser = (Button)findViewById(R.id.addbut);
register.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater inflator=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupview=inflator.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupview, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(popupview, Gravity.CENTER, 0, 0);
popupWindow.setFocusable(true);
popupWindow.update();
Button dismissbtn = (Button)popupview.findViewById(R.id.close);
dismissbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(register, 50, -30);
}
});
openDB();
}
public void openDB(){
myDB = new DBAdapter(this);
myDB.open();
}
private void closeDB() {
myDB.close();
}
private void displayText(String message) {
TextView textView = (TextView) findViewById(R.id.textdisp);
textView.setText(message);
}
public void registerUser(View v) {
EditText username = (EditText)findViewById(R.id.usertext);
EditText password = (EditText)findViewById(R.id.passwordtext);
EditText weight = (EditText)findViewById(R.id.weight);
String getUsername = username.getText().toString();
String getPassword = password.getText().toString();
int getWeight = Integer.parseInt(weight.getText().toString());
long newId = myDB.insertRow(getUsername, getPassword, getWeight);
Cursor cursor = myDB.getRow(newId);
displayRecordSet(cursor);
}
public void onClick_ClearAll(View v) {
myDB.deleteAll();
}
public void onClick_DisplayRecords(View v) {
Cursor cursor = myDB.getAllRows();
displayRecordSet(cursor);
}
private void displayRecordSet(Cursor cursor) {
String message = "";
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(DBAdapter.COL_ROWID);
String name = cursor.getString(DBAdapter.COL_NAME);
String password = cursor.getString(DBAdapter.COL_PASSWORD);
int weight = Integer.parseInt(cursor.getString(DBAdapter.COL_WEIGHT));
message += "id=" + id
+", name=" + name
+", #=" + password
+", Colour=" + weight
+"\n";
} while(cursor.moveToNext());
}
cursor.close();
displayText(message);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.current_users, menu);
return true;
}
}
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.16" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="220dp"
android:text="@string/title_activity_current_users"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="100dp"
android:text="@string/name" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="200dp"
android:paddingTop="100dp"
android:text="@string/timein" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="400dp"
android:paddingTop="100dp"
android:text="@string/score" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="540dp"
android:paddingTop="100dp"
android:text="@string/done" />
<Button
android:id="@+id/regbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register" />
<Button
android:id="@+id/addbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="505dp"
android:onClick="onClick_ClearAll"
android:text="@string/adduser" />
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textdisp"
android:layout_width="match_parent"
android:layout_height="2000dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
PopupWindow 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"
android:background="@android:color/darker_gray" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register"
android:textSize="20sp" />
<TextView
android:text="@string/desireduser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/usertext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
/>
<TextView
android:text="@string/desiredpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/passwordtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
/>
<TextView
android:text="@string/weightinpounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="registerUser"
android:text="@string/register2" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close" />
</LinearLayout>
我已经尝试过不使用editTexts,但它仍然是相同的。请帮助我谢谢!
编辑这是logcat(抱歉)
08-18 14:42:43.062: E/AndroidRuntime(9748): FATAL EXCEPTION: main
08-18 14:42:43.062: E/AndroidRuntime(9748): java.lang.IllegalStateException: Could not execute method of the activity
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.view.View$1.onClick(View.java:3599)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.view.View.performClick(View.java:4204)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.view.View$PerformClick.run(View.java:17355)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.os.Handler.handleCallback(Handler.java:725)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.os.Looper.loop(Looper.java:137)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-18 14:42:43.062: E/AndroidRuntime(9748): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:42:43.062: E/AndroidRuntime(9748): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 14:42:43.062: E/AndroidRuntime(9748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-18 14:42:43.062: E/AndroidRuntime(9748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-18 14:42:43.062: E/AndroidRuntime(9748): at dalvik.system.NativeStart.main(Native Method)
08-18 14:42:43.062: E/AndroidRuntime(9748): Caused by: java.lang.reflect.InvocationTargetException
08-18 14:42:43.062: E/AndroidRuntime(9748): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:42:43.062: E/AndroidRuntime(9748): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 14:42:43.062: E/AndroidRuntime(9748): at android.view.View$1.onClick(View.java:3594)
08-18 14:42:43.062: E/AndroidRuntime(9748): ... 11 more
08-18 14:42:43.062: E/AndroidRuntime(9748): Caused by: java.lang.NullPointerException
08-18 14:42:43.062: E/AndroidRuntime(9748): at com.example.istronggyminstructor.CurrentUsers.registerUser(CurrentUsers.java:75)
08-18 14:42:43.062: E/AndroidRuntime(9748): ... 14 more
答案 0 :(得分:2)
可能myDB
为空(因为您没有给出完整的堆栈跟踪)。
在执行任何数据库操作之前,请打开数据库。因此,在进行任何与DB相关的操作之前,需要调用方法openDB()
。
在不初始化myDB
的情况下,您正在进行数据库操作,即myDB.insertRow(getUsername, getPassword, getWeight);
和Cursor cursor = myDB.getRow(newId);
。此处myDB
为空,因为您尚未对其进行初始化,即myDB = new DBAdapter(this)
修改强>
您有2个选项。
1)使View popupview
变量全局变量。然后替换
View popupview=inflator.inflate(R.layout.popup, null);
通过
popupview=inflator.inflate(R.layout.popup, null);
然后在方法registerUser(View v)
内,替换
EditText username = (EditText)findViewById(R.id.usertext);
EditText password = (EditText)findViewById(R.id.passwordtext);
EditText weight = (EditText)findViewById(R.id.weight);
通过
EditText username = (EditText)popupview.findViewById(R.id.usertext);
EditText password = (EditText)popupview.findViewById(R.id.passwordtext);
EditText weight = (EditText)popupview.findViewById(R.id.weight);
2)移动
EditText username = (EditText)findViewById(R.id.usertext);
EditText password = (EditText)findViewById(R.id.passwordtext);
EditText weight = (EditText)findViewById(R.id.weight);
来自registerUser(View v)
并将其粘贴在
Button dismissbtn = (Button)popupview.findViewById(R.id.close);
所以看起来应该是这样的
Button dismissbtn = (Button)popupview.findViewById(R.id.close);
EditText username = (EditText)popupview.findViewById(R.id.usertext);
EditText password = (EditText)popupview.findViewById(R.id.passwordtext);
EditText weight = (EditText)popupview.findViewById(R.id.weight);