Android/Java コードに問題があります。あるアクティビティから別のアクティビティに切り替えると、単にエラーが発生します。私が確信している問題はデータベースです。スタック トレースの主なエラーは、アクティビティの null ポインターと、データベースが明示的に閉じられていないことです。この問題に関して誰か助けてもらえますか?
以下のエラートレースに詳しい人はいますか?
問題は、取得したいページに到達する方法が 2 つあります。ショートカットの方法を使用すると、最初にアプリをロードしたときに次のボタンでエラーが表示されます。しかし、編集を行うと、目的のページにアクセスして戻ってきます。最初の「次」が機能します。それはとても迷惑です!
これは logcat のトレースです:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sec/com.android.sec.TabLayoutActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sec/com.android.sec.RoomOverview}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
04-15 20:37:05.333: E/AndroidRuntime(568): at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.sec/com.android.sec.RoomOverview}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:213)
at com.android.sec.TabLayoutActivity.onCreate(TabLayoutActivity.java:30)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
E/AndroidRuntime(568): Caused by: java.lang.NullPointerException
at com.android.sec.RoomOverview.onCreate(RoomOverview.java:106)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(568): ... 20 more
エラーをスローしているクラスは次のとおりです。
package com.android.sec;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
public class RoomOverview extends Activity {
public String value = CreateClient.getName();
public String status = CreateRoom.getStatus();
private ListView lvItem;
private ArrayList<String> itemArrey;
private ArrayAdapter<String> itemAdapter;
public Cursor c, c2, c3, c4, c5, c6;
SQLiteDatabase sampleDB;
public static String selectedItem;
ImageButton edit, delete;
final Context context = this;
public String typeToDelete;
public static String getID[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.roomoverview);
edit = (ImageButton) findViewById(R.id.editButton);
edit.setEnabled(false);
edit.setAlpha(127);
delete = (ImageButton) findViewById(R.id.deleteButton);
delete.setEnabled(false);
delete.setAlpha(127);
lvItem = (ListView)this.findViewById(R.id.listView1);
lvItem.setTextFilterEnabled(true);
itemArrey = new ArrayList<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.single_item,itemArrey);
lvItem.setAdapter(itemAdapter);
if(value == "Retrieve"){
sampleDB = getBaseContext().openOrCreateDatabase(ClientList.retrievedClient+".db", MODE_PRIVATE, null);
}
else if (value == "Create"){
sampleDB = getBaseContext().openOrCreateDatabase(CreateClient.createdClient+".db", MODE_PRIVATE, null);
}
if(status == "Edit"){
TextView roomName = (TextView) findViewById(R.id.roomName);
roomName.append(EditRoom.possiblyEditedRoom);
c = sampleDB.rawQuery("SELECT * FROM tbl_plant WHERE r_id =?", new String[] {RoomList.r_id});
c2 = sampleDB.rawQuery("SELECT * FROM tbl_windowData WHERE r_id =?", new String[] {RoomList.r_id});
c3 = sampleDB.rawQuery("SELECT * FROM tbl_itData WHERE r_id =?", new String[] {RoomList.r_id});
c4 = sampleDB.rawQuery("SELECT * FROM tbl_otherData WHERE r_id =?", new String[] {RoomList.r_id});
c5 = sampleDB.rawQuery("SELECT * FROM tbl_heatingData WHERE r_id =?", new String[] {RoomList.r_id});
c6 = sampleDB.rawQuery("SELECT * FROM tbl_lightingData WHERE r_id =?", new String[] {RoomList.r_id});
}
else if (status == "Create"){
TextView roomName = (TextView) findViewById(R.id.roomName);
roomName.append(CreateRoom.selectedRoom);
c = sampleDB.rawQuery("SELECT * FROM tbl_plant WHERE r_id =?", new String[] {CreateRoom.r_id});
c2 = sampleDB.rawQuery("SELECT * FROM tbl_windowData WHERE r_id =?", new String[] {CreateRoom.r_id});
c3 = sampleDB.rawQuery("SELECT * FROM tbl_itData WHERE r_id =?", new String[] {CreateRoom.r_id});
c4 = sampleDB.rawQuery("SELECT * FROM tbl_otherData WHERE r_id =?", new String[] {CreateRoom.r_id});
c5 = sampleDB.rawQuery("SELECT * FROM tbl_heatingData WHERE r_id =?", new String[] {CreateRoom.r_id});
c6 = sampleDB.rawQuery("SELECT * FROM tbl_lightingData WHERE r_id =?", new String[] {CreateRoom.r_id});
}
itemArrey.add("---------- Plant ----------");
if (c != null ) {
if (c.moveToFirst()) {
do {
String id = c.getString(c.getColumnIndex("p_id"));
String plantName = c.getString(c.getColumnIndex("itemName"));
String plantType = c.getString(c.getColumnIndex("itemType"));
itemArrey.add("Plant Name: "+plantName+"\n"+"Type: "+plantType+"\n"+"ID: #"+id);
}while (c.moveToNext());
}
}
c.close();
Multiple of the above i.e. c2, c3, c4 etc. etc. - saves reading
lvItem.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
selectedItem = (String) ((TextView) view).getText();
if(selectedItem.contains("----------")){}
else {
edit.setEnabled(true);
edit.setAlpha(255);
delete.setEnabled(true);
delete.setAlpha(255);
//Toast.makeText(getApplicationContext(), "item: "+selectedItem+" child: "+parent.getCount(), Toast.LENGTH_LONG).show();
for(int a = 0; a < parent.getChildCount(); a++)
{
parent.getChildAt(a).setBackgroundColor(Color.WHITE);
}
view.setBackgroundColor(Color.parseColor("#97C361"));
}
}
});
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(selectedItem.contains("----------")){}
else {
sampleDB.close();
if(selectedItem.startsWith("Plant Name:")){
typeToDelete = "P";
}
else if (selectedItem.startsWith("Type:")){
typeToDelete = "W";
}
else if (selectedItem.startsWith("PC Quantity:")){
typeToDelete = "I";
}
else if (selectedItem.startsWith("Other Name:")){
typeToDelete = "O";
}
else if (selectedItem.startsWith("Heating Type:")){
typeToDelete = "H";
}
else if (selectedItem.startsWith("Lighting Type:")){
typeToDelete = "L";
}
getID = selectedItem.split("#");
if(typeToDelete == "P"){
Intent intent2 = new Intent(RoomOverview.this, EditPlant.class);
startActivityForResult(intent2, 0);
}
else if (typeToDelete == "W"){
Intent intent2 = new Intent(RoomOverview.this, EditWindows.class);
startActivityForResult(intent2, 0);
}
else if (typeToDelete == "I"){
Intent intent2 = new Intent(RoomOverview.this, EditIT.class);
startActivityForResult(intent2, 0);
}
else if (typeToDelete == "O"){
Intent intent2 = new Intent(RoomOverview.this, EditOther.class);
startActivityForResult(intent2, 0);
}
else if (typeToDelete == "H"){
Intent intent2 = new Intent(RoomOverview.this, EditHeating.class);
startActivityForResult(intent2, 0);
}
else if (typeToDelete == "L"){
Intent intent2 = new Intent(RoomOverview.this, EditLighting.class);
startActivityForResult(intent2, 0);
}
}
}
});
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(selectedItem.contains("----------")){}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Delete:");
alertDialogBuilder.setMessage("Are you sure?").setCancelable(false).setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//delete building
if(selectedItem.startsWith("Plant Name:")){
typeToDelete = "P";
}
else if (selectedItem.startsWith("Type:")){
typeToDelete = "W";
}
else if (selectedItem.startsWith("PC Quantity:")){
typeToDelete = "I";
}
else if (selectedItem.startsWith("Other Name:")){
typeToDelete = "O";
}
else if (selectedItem.startsWith("Heating Type:")){
typeToDelete = "H";
}
else if (selectedItem.startsWith("Lighting Type:")){
typeToDelete = "L";
}
String getID[] = selectedItem.split("#");
if(typeToDelete == "P"){
sampleDB.delete("tbl_plant", "p_id =?", new String[] {getID[1]});
}
else if (typeToDelete == "W"){
sampleDB.delete("tbl_windowData", "w_id =?", new String[] {getID[1]});
}
else if (typeToDelete == "I"){
sampleDB.delete("tbl_itData", "i_id =?", new String[] {getID[1]});
}
else if (typeToDelete == "O"){
sampleDB.delete("tbl_otherData", "o_id =?", new String[] {getID[1]});
}
else if (typeToDelete == "H"){
sampleDB.delete("tbl_heatingData", "h_id =?", new String[] {getID[1]});
}
else if (typeToDelete == "L"){
sampleDB.delete("tbl_lightingData", "l_id =?", new String[] {getID[1]});
}
sampleDB.close();
Intent intent2 = new Intent(RoomOverview.this, TabLayoutActivity.class);
startActivityForResult(intent2, 0);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}});
}
}