gpt4 book ai didi

android fragment sqlite查询崩溃

转载 作者:行者123 更新时间:2023-12-03 17:22:10 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Unfortunately MyApp has stopped. How can I solve this?

(22 个回答)


6年前关闭。




我已经成功使用了一段时间的 sqlite 查询。但是,当我将这些代码迁移到另一个 APP 的 fragment 中时。查询后代码将立即崩溃。进入这些代码时,变量似乎没问题。我的猜测是“上下文”之类的东西没有给出正确的值,但是,我找不到它。以下是代码,请帮助我找到问题。

问题已解决:忘记将​​文件从 Assets 复制到数据库目录。

fragment .java

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class Fragment1 extends Fragment {

private int position;
private ListView lv;
private static dbOperator dbOp;
private Cursor cursor;
private Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

context=getActivity();

View rootView = inflater.inflate(R.layout.fragment_list, container, false);
lv=(ListView) rootView.findViewById(R.id.list);
dbOp = new dbOperator(context);
dbOp.open();

fillListChatrooms();

return rootView;

}


@Override
public void onDestroyView(){
dbOp.close();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

private void fillListChatrooms() {

int visibility;

cursor = dbOp.readChatrooms();
ListAdapterChatrooms adapter = new ListAdapterChatrooms(context, cursor, dbOp);

lv.setAdapter(adapter);
}
}

dbOperator.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import java.io.IOException;
import java.util.Locale;

public class dbOperator {


private static final String dbName_friends = "friends.db";

private static final int dbVersion= 1;

public static final String tableCategories = "categories";
public static final String columnIDC = "id_c";

public static final String tableInterests = "interests";
public static final String columnIDI = "id_i";


public static final String columnIDU = "id_u";
public static final String columnName = "name";
public static final String columnLastChatTimestamp = "last_chat_timestamp";
public static final String columnMatchedPercentage = "matched_percentage";
public static final String columnIcon = "icon";
public static final String columnChatLog = "chat_log";


public static final String columnIDD = "id_d";
public static final String columnTimestamp = "timestamp";
public static final String columnContentFile = "content_file";

public static final String tableChatrooms = "chatrooms";
public static final String columnIDCR = "id_cr";
public static final String columnChatType = "type";
public static final String columnUnreadMessages = "unread_messages";


private String language;

private final Context context;
private dbOpenHelper dbHelper_friends;
private SQLiteDatabase db_friends;

private final String sdPath;

private String dbPath_friends ;

private static boolean flagDbModified_friends=false;


dbOperator(Context ctx){
context=ctx;


dbPath_friends = ctx.getDatabasePath(dbName_friends).getPath();

sdPath= Environment.getExternalStorageDirectory().getPath();
Locale current= ctx.getResources().getConfiguration().locale;
language=current.getCountry();

switch (language)
{
case "de":
break;
case "zh-rTW":
language="tw";
break;
default:
language="en";
break;
}

}

public String getLanguage()
{
return language;
}

public void open() throws SQLException {

boolean flagDBexist;

flagDBexist=fileOp.checkFileExist(dbPath_friends);
if (!flagDBexist ) copyDBfromServer(dbName_friends);
dbHelper_friends= new dbOpenHelper(context, dbName_friends );
db_friends=dbHelper_friends.getWritableDatabase();


}

public void close() {

dbHelper_friends.close();
}

private void copyDBfromServer(String dbName)
{

}

private void backupDB(String source, String target) throws IOException{

fileOp.copyFile(source, target);

}

public Cursor readChatrooms(){

Cursor c;
// Below is where the crash start
c=db_friends.query(tableChatrooms,null,null,null,null,null,columnChatType);

return c;

}




public class dbOpenHelper extends SQLiteOpenHelper {

public dbOpenHelper(Context context, String dbName) {
super(context, dbName, null, dbVersion);
// super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase sqldb) {
// TODO Auto-generated method stub



}


@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
}
}

最佳答案

调用 fragment 的 onActivityCreated() 方法

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

context=getActivity()
dbOp = new dbOperator(context);
dbOp.open();
fillListChatrooms();
}

关于android fragment sqlite查询崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com