我正在尝试使用 android 为作业构建时间表应用程序。我不断收到空指针异常以及调用目标异常。我意识到 nullpointerexception 通常是在变量未正确初始化时引起的,但我看不出我的代码哪里出错了。
字段填写在一个 xml 文件中,用户单击保存按钮,下面会调用“addNewModule”方法。错误是在“queryValuesMap.put("ModuleCode", ModuleCode.getText().toString())" 行中生成的。我添加了一段我的 DBTools 文件以防万一。
新模块.java
package com.example.mycoursetimetable;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class NewModule extends Activity{
// The EditText objects
EditText ModuleCode;
EditText ModuleName;
EditText ModuleType;
EditText DayOfWeek;
EditText StartTime;
EditText EndTime;
EditText Location;
EditText AdditionalInfo;
DBTools dbTools;
@Override
public void onCreate(Bundle savedInstanceState) {
dbTools = new DBTools(this);
// Get saved data if there is any
super.onCreate(savedInstanceState);
// Designate that add_module.xml is the interface used
setContentView(R.layout.add_module);
// Initialize the EditText objects
ModuleCode= (EditText) findViewById(R.id.modcodeet);
ModuleName = (EditText) findViewById(R.id.modnameet);
ModuleType = (EditText) findViewById(R.id.moduletypeet);
DayOfWeek = (EditText) findViewById(R.id.dowet);
StartTime = (EditText) findViewById(R.id.starttimeet);
EndTime = (EditText) findViewById(R.id.endtimeet);
Location = (EditText) findViewById(R.id.locationet);
AdditionalInfo = (EditText) findViewById(R.id.additionalinfoet);
}
public void addNewModule(View view) {
// Will hold the HashMap of values
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
// Get the values from the EditText boxes
queryValuesMap.put("ModuleCode", ModuleCode.getText().toString());// This si where error occurs
queryValuesMap.put("ModuleName", ModuleName.getText().toString());
queryValuesMap.put("ModuleType", ModuleType.getText().toString());
queryValuesMap.put("DayOfWeek", DayOfWeek.getText().toString());
queryValuesMap.put("StartTime", StartTime.getText().toString());
queryValuesMap.put("EndTime", EndTime.getText().toString());
queryValuesMap.put("Location", Location.getText().toString());
queryValuesMap.put("AdditionalInfo", AdditionalInfo.getText().toString());
// Call for the HashMap to be added to the database
dbTools.insertModule(queryValuesMap);
// Call for MainActivity to execute
this.callMainActivity(view);
}
public void callMainActivity(View view) {
Intent theIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(theIntent);
}
}
DBTools.java
package com.example.mycoursetimetable;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBTools extends SQLiteOpenHelper {
public DBTools(Context applicationContext){
super(applicationContext, "module.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String query = "CREATE TABLE modules ( ModuleCode TEXT PRIMARY KEY, ModuleName TEXT,ModuleType TEXT, DayOfWeek TEXT, StartTime TEXT, EndTime TEXT,Location TEXT,AdditionalInfo Text)";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS modules";
database.execSQL(query);
onCreate(database);
}
public void insertModule(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ModuleCode", queryValues.get("ModuleCode"));
values.put("ModuleName", queryValues.get("ModuleName"));
values.put("ModuleType", queryValues.get("ModuleType"));
values.put("DayOfWeek", queryValues.get("DayOfWeek"));
values.put("StartTime", queryValues.get("StartTime"));
values.put("EndTime", queryValues.get("EndTime"));
values.put("Location", queryValues.get("Location"));
values.put("AdditionalInfo", queryValues.get("AdditionalInfo"));
database.insert("modules", null, values);
database.close();
}
MainActivity.java
package com.example.mycoursetimetable;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
// The Intent is used to issue that an operation should
// be performed
Intent intent;
TextView ModuleCode;
// The object that allows me to manipulate the database
DBTools dbTools = new DBTools(this);
// Called when the Activity is first called
protected void onCreate(Bundle savedInstanceState) {
// Get saved data if there is any
super.onCreate(savedInstanceState);
// Designate that edit_module.xml is the interface used
// is activity_main.xml
setContentView(R.layout.activity_main);
// Gets all the data from the database and stores it
// in an ArrayList
ArrayList<HashMap<String, String>> moduleList = dbTools.getAllModules();
// Check to make sure there are contacts to display
if(moduleList.size()!=0) {
// Get the ListView and assign an event handler to it
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When an item is clicked get the TextView
// with a matching checkId
ModuleCode = (TextView) view.findViewById(R.id.moduleCode);
// Convert that ModuleId into a String
String ModuleID = ModuleCode.getText().toString();
// Signals an intention to do something
// getApplication() returns the application that owns
// this activity
Intent theIndent = new Intent(getApplication(),EditModule.class);
// Put additional data in for EditContact to use
theIndent.putExtra("ModuleCode", ModuleID);
// Calls for EditModule
startActivity(theIndent);
}
});
// A list adapter is used bridge between a ListView and
// the ListViews data
// The SimpleAdapter connects the data in an ArrayList
// to the XML file
// First we pass in a Context to provide information needed
// about the application
// The ArrayList of data is next followed by the xml resource
// Then we have the names of the data in String format and
// their specific resource ids
ListAdapter adapter = new SimpleAdapter( MainActivity.this,moduleList, R.layout.module_entry, new String[] { "ModuleCode","ModuleName", "moduleType"}, new int[] {R.id.moduleCode, R.id.moduleName, R.id.ModuleType});
// setListAdapter provides the Cursor for the ListView
// The Cursor provides access to the database data
setListAdapter(adapter);
}
}
// When showAddContact is called with a click the Activity
// NewModule is called
public void showAddModule(View view) {
Intent theIntent = new Intent(getApplicationContext(), NewModule.class);
startActivity(theIntent);
}
}
add_module.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_5dp"
android:text="@string/add_module"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/modulecodetv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/module_code" />
<EditText
android:id="@+id/modulecodeet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/modulenametv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/module_name" />
<EditText
android:id="@+id/modulenameet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/moduletypetv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/module_type" />
<EditText
android:id="@+id/moduletypeet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/dowtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/dow" />
<EditText
android:id="@+id/dowet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/starttimetv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/start_time" />
<EditText
android:id="@+id/starttimeet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/endtimetv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/end_time" />
<EditText
android:id="@+id/endtimeet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/locationtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/location" />
<EditText
android:id="@+id/locationet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow9"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/additionalinfotv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="@dimen/padding_5dp"
android:text="@string/additional_info" />
<EditText
android:id="@+id/additionalinfoet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:paddingRight="@dimen/padding_5dp" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow10"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/addbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="addNewModule"
android:text="@string/save_data" />
</TableRow>
</TableLayout>
日志猫
10-22 11:34:56.158: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 11:34:56.158: E/AndroidRuntime(1765): at dalvik.system.NativeStart.main(Native Method)
10-22 11:34:56.158: E/AndroidRuntime(1765): Caused by: java.lang.reflect.InvocationTargetException
10-22 11:34:56.158: E/AndroidRuntime(1765): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:34:56.158: E/AndroidRuntime(1765): at java.lang.reflect.Method.invoke(Method.java:525)
10-22 11:34:56.158: E/AndroidRuntime(1765): at android.view.View$1.onClick(View.java:3628)
10-22 11:34:56.158: E/AndroidRuntime(1765): ... 11 more
10-22 11:34:56.158: E/AndroidRuntime(1765): Caused by: java.lang.NullPointerException
10-22 11:34:56.158: E/AndroidRuntime(1765): at com.example.mycoursetimetable.NewModule.addNewModule(NewModule.java:78)
10-22 11:34:56.158: E/AndroidRuntime(1765): ... 14 more
10-22 11:34:56.438: D/dalvikvm(1765): GC_FOR_ALLOC freed 454K, 14% free 3272K/3788K, paused 96ms, total 136ms
10-22 11:39:56.627: I/Process(1765): Sending signal. PID: 1765 SIG: 9
10-22 11:39:59.818: D/dalvikvm(1791): GC_FOR_ALLOC freed 81K, 5% free 2938K/3080K, paused 146ms, total 159ms
10-22 11:39:59.818: I/Choreographer(1791): Skipped 41 frames! The application may be doing too much work on its main thread.
10-22 11:40:00.268: I/Choreographer(1791): Skipped 264 frames! The application may be doing too much work on its main thread.
10-22 11:40:00.437: D/gralloc_goldfish(1791): Emulator without GPU emulation detected.
10-22 11:40:01.157: I/Choreographer(1791): Skipped 193 frames! The application may be doing too much work on its main thread.
10-22 11:46:11.667: I/Choreographer(1835): Skipped 37 frames! The application may be doing too much work on its main thread.
10-22 11:46:11.867: D/dalvikvm(1835): GC_FOR_ALLOC freed 82K, 5% free 2937K/3080K, paused 99ms, total 108ms
10-22 11:46:12.187: I/Choreographer(1835): Skipped 324 frames! The application may be doing too much work on its main thread.
10-22 11:46:12.287: D/gralloc_goldfish(1835): Emulator without GPU emulation detected.
10-22 11:46:12.607: I/Choreographer(1835): Skipped 85 frames! The application may be doing too much work on its main thread.
10-22 11:46:17.358: I/Choreographer(1835): Skipped 30 frames! The application may be doing too much work on its main thread.
10-22 11:46:19.138: I/Choreographer(1835): Skipped 132 frames! The application may be doing too much work on its main thread.
10-22 11:46:21.387: I/Choreographer(1835): Skipped 184 frames! The application may be doing too much work on its main thread.
10-22 11:46:24.517: D/dalvikvm(1835): GC_FOR_ALLOC freed 247K, 9% free 3204K/3512K, paused 54ms, total 68ms
10-22 11:46:25.689: I/Choreographer(1835): Skipped 76 frames! The application may be doing too much work on its main thread.
10-22 11:46:28.367: I/Choreographer(1835): Skipped 236 frames! The application may be doing too much work on its main thread.
10-22 11:46:28.857: I/Choreographer(1835): Skipped 129 frames! The application may be doing too much work on its main thread.
10-22 11:46:31.287: I/Choreographer(1835): Skipped 52 frames! The application may be doing too much work on its main thread.
10-22 11:46:31.757: I/Choreographer(1835): Skipped 126 frames! The application may be doing too much work on its main thread.
10-22 11:46:32.157: D/dalvikvm(1835): GC_FOR_ALLOC freed 445K, 14% free 3270K/3776K, paused 52ms, total 65ms
10-22 11:46:32.217: I/Choreographer(1835): Skipped 97 frames! The application may be doing too much work on its main thread.
10-22 11:46:33.297: I/Choreographer(1835): Skipped 112 frames! The application may be doing too much work on its main thread.
10-22 11:46:35.288: I/Choreographer(1835): Skipped 89 frames! The application may be doing too much work on its main thread.
10-22 11:46:35.597: I/Choreographer(1835): Skipped 31 frames! The application may be doing too much work on its main thread.
10-22 11:46:38.027: I/Choreographer(1835): Skipped 79 frames! The application may be doing too much work on its main thread.
10-22 11:46:38.277: I/Choreographer(1835): Skipped 31 frames! The application may be doing too much work on its main thread.
10-22 11:46:39.397: D/dalvikvm(1835): GC_FOR_ALLOC freed 505K, 15% free 3279K/3844K, paused 53ms, total 61ms
10-22 11:46:40.117: I/Choreographer(1835): Skipped 62 frames! The application may be doing too much work on its main thread.
10-22 11:46:42.928: D/AndroidRuntime(1835): Shutting down VM
10-22 11:46:43.027: W/dalvikvm(1835): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-22 11:46:43.358: E/AndroidRuntime(1835): FATAL EXCEPTION: main
10-22 11:46:43.358: E/AndroidRuntime(1835): java.lang.IllegalStateException: Could not execute method of the activity
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.view.View$1.onClick(View.java:3633)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.view.View.performClick(View.java:4240)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.view.View$PerformClick.run(View.java:17721)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.os.Handler.handleCallback(Handler.java:730)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.os.Handler.dispatchMessage(Handler.java:92)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.os.Looper.loop(Looper.java:137)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 11:46:43.358: E/AndroidRuntime(1835): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:46:43.358: E/AndroidRuntime(1835): at java.lang.reflect.Method.invoke(Method.java:525)
10-22 11:46:43.358: E/AndroidRuntime(1835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 11:46:43.358: E/AndroidRuntime(1835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 11:46:43.358: E/AndroidRuntime(1835): at dalvik.system.NativeStart.main(Native Method)
10-22 11:46:43.358: E/AndroidRuntime(1835): Caused by: java.lang.reflect.InvocationTargetException
10-22 11:46:43.358: E/AndroidRuntime(1835): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 11:46:43.358: E/AndroidRuntime(1835): at java.lang.reflect.Method.invoke(Method.java:525)
10-22 11:46:43.358: E/AndroidRuntime(1835): at android.view.View$1.onClick(View.java:3628)
10-22 11:46:43.358: E/AndroidRuntime(1835): ... 11 more
10-22 11:46:43.358: E/AndroidRuntime(1835): Caused by: java.lang.NullPointerException
10-22 11:46:43.358: E/AndroidRuntime(1835): at com.example.mycoursetimetable.NewModule.addNewModule(NewModule.java:59)
10-22 11:46:43.358: E/AndroidRuntime(1835): ... 14 more
10-22 11:46:49.328: I/Process(1835): Sending signal. PID: 1835 SIG: 9
10-22 11:46:52.637: I/Choreographer(1864): Skipped 131 frames! The application may be doing too much work on its main thread.
10-22 11:46:52.707: D/gralloc_goldfish(1864): Emulator without GPU emulation detected.
10-22 11:46:53.237: I/Choreographer(1864): Skipped 153 frames! The application may be doing too much work on its main thread.