在 json 中插入/显示/加载崩溃



首先:1.当我将空数据插入sql时,它崩溃了2。当加载 json 崩溃 3.按下空数据显示记录崩溃 请帮忙!

这是主要活动:

package com.example.user.notebook;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.example.user.notebook.Students;

public class MainActivity extends Activity {
LinearLayout mainLayout=null;
EditText lessons=null,student=null,grade=null,observations=null;
Button insertRecord=null,showRecords=null;
ArrayList<Students> result= new ArrayList<>();
TableLayout resultLayout=null;
Database db=null;
LinearLayout jsonLayout=null;
Button loadJSON=null,saveJSON=null;
public void makeJSON()
{
jsonLayout=new LinearLayout(this);
mainLayout.addView(jsonLayout);
loadJSON=new Button(this);
loadJSON.setText("LOAD JSON");
jsonLayout.addView(loadJSON);
loadJSON.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
AlertDialog.Builder alert = new
AlertDialog.Builder(MainActivity.this);
alert.setTitle("Load FILE");
alert.setMessage("Specify file name: ");
final EditText input = new EditText(MainActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String value = input.getText().toString();
File myfile=new File(
Environment.getExternalStorageDirectory(),value);
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(new
FileInputStream(myfile), "utf8"),65536);
String line="";
line=br.readLine();
try {
JSONArray x=new JSONArray(line);
Log.d("TEST","I have read "+x);
int i;
for(i=0;i<x.length();i++)
{
JSONObject p=x.getJSONObject(i);
String lessons=p.getString("lessons");
String student=p.getString("student");
String observations=p.getString("observations");
double grade =p.getDouble("grade");
db.insert(lessons, student, observations, grade);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
br.close();
}catch(IOException e)
{
Log.d("TEST",e.getMessage());
}
}});
alert.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
}
});
alert.show();
}
});
saveJSON=new Button(this);
saveJSON.setText("SAVE JSON");
saveJSON.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
result=db.getResults();
final JSONArray x=new JSONArray();
int i;
for(i=0;i<result.size();i++)
{
JSONObject p=new JSONObject();
try {
p.put("lessons", result.get(i).lessons);
p.put("student",result.get(i).student);
p.put("observations", result.get(i).observations);
p.put("grade", result.get(i).grade);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x.put(p);
}
String s=x.toString();
AlertDialog.Builder alert = new
AlertDialog.Builder(MainActivity.this);
alert.setTitle("Create FILE");
alert.setMessage("Specify file name: ");
final EditText input = new EditText(MainActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String value = input.getText().toString();
File myfile=new File(
Environment.getExternalStorageDirectory(),value);
try {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(myfile), "UTF8"));
out.append(x.toString());
out.flush();
out.close();
Log.d("TEST", "Write "+x);
}catch(IOException e)
{
Log.d("TEST",e.getMessage());
}
}});
alert.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
}
});
alert.show();
}

});
jsonLayout.addView(saveJSON);
}
public void makeInputs()
{
LinearLayout l1=new LinearLayout(this);
mainLayout.addView(l1);
lessons=new EditText(this);
lessons.setHint("lessons");
l1.addView(lessons);
student=new EditText(this);
student.setHint("student");
l1.addView(student);
observations=new EditText(this);
observations.setHint("observations");
l1.addView(observations);
grade=new EditText(this);
l1.addView(grade);
grade.setHint("grade");
}
public void makeButtons()
{
LinearLayout l2=new LinearLayout(this);
mainLayout.addView(l2);
insertRecord=new Button(this);
insertRecord.setText("INSERT RECORD");
l2.addView(insertRecord);
showRecords=new Button(this);
showRecords.setText("SHOW RECORDS");
l2.addView(showRecords);
insertRecord.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
db.insert(lessons.getText().toString(),
student.getText().toString(),
observations.getText().toString(),
Double.parseDouble(grade.getText().toString()));
}
});
showRecords.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
result=db.getResults();
updateTable();
}
});
}
public void makeTable()
{
resultLayout=new TableLayout(this);
ScrollView scroll=new ScrollView(this);
mainLayout.addView(scroll);
scroll.addView(resultLayout);
TableRow r1=new TableRow(this);
resultLayout.addView(r1);
}
public void updateTable()
{
resultLayout.removeAllViews();
makeTable();
int i;
for(i=0;i<result.size();i++)
{
Students c=result.get(i);
TableRow r=new TableRow(this);
resultLayout.addView(r);
TextView t1,t2,t3,t4;
t1=new TextView(this);
t1.setText(c.lessons);
t2=new TextView(this);
t2.setText(c.student);
t3=new TextView(this);
t3.setText(c.observations);
t4=new TextView(this);
t4.setText(""+c.grade);
r.addView(t1);
r.addView(t2);
r.addView(t3);
r.addView(t4);
ImageView delimage=new ImageView(this);
r.addView(delimage);
delimage.setId(i);
delimage.setImageResource(R.drawable.remove);
delimage.setClickable(true);
delimage.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
String cardetails="lessons: "+
result.get(v.getId()).lessons+
" lessons: "+
result.get(v.getId()).student+
" student: "+
result.get(v.getId()).observations+
"observations: "+
result.get(v.getId()).grade+
" grade: ";
Log.d("TEST","Delete school is "+cardetails);
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout=new LinearLayout(this);
setContentView(mainLayout);
mainLayout.setOrientation(LinearLayout.VERTICAL);
db=new Database(this, "school.db", null, 2);
makeInputs();
makeButtons();
makeJSON();
makeTable();
}
}

数据库

package com.example.user.notebook;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper{
private Context mcontext;
private SQLiteDatabase database;
public Database(Context context, String name, CursorFactory factory,
int version) {
super(context, name, null, version);
mcontext=context;
database=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table school(lessons text,student text,observations text,grade double)");
}
public ArrayList<Students> getResults()
{
ArrayList<Students> x= new ArrayList<Students>();
Cursor cursor=database.rawQuery("select * from school",null);
if(cursor.getCount()==0)
{
cursor.close();
return x;
}
int lessonsindex=cursor.getColumnIndex("lessons");
int studentindex=cursor.getColumnIndex("student");
int observationsindex=cursor.getColumnIndex("observations");
int gradeindex=cursor.getColumnIndex("grade");
cursor.moveToFirst();
do
{
Students c;
c=new Students(cursor.getString(lessonsindex),
cursor.getString(studentindex),
cursor.getString(observationsindex),
cursor.getDouble(gradeindex));
x.add(c);
}while(cursor.moveToNext());
cursor.close();
return null;
}
public void delete(String lessons,String student, String observations, double grade)
{
database.execSQL("delete from school where lessons='"+lessons+"' and student='"+
student+"'and observations='"+observations+"' and grade = '"+grade);
}
public void insert(String lessons,String student, String observations, double grade)
{
database.execSQL("insert into school(lessons,student,observations,grade) values('"+
lessons+"','"+student+"','"+observations+"','"+grade+"')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table school");
onCreate(db);
}
public void clearData()
{
database.execSQL("delete from school");
}
}

日志猫:

01-24 22:10:49.962 2421-2421/?E/受精卵:v2 01-24 22:10:49.972 2421-2421/?E/SELinux: [调试] get_category: 变量 seinfo: 默认敏感度: 空, 分类: 空 01-24 22:14:42.292 2421-2421/com.example.user.notebook E/Android运行时:致命 异常:主要 进程: com.example.user.notebook, PID: 2421 java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'int java.util.ArrayList.size()' at com.example.user.notebook.MainActivity.updateTable(MainActivity.java:257) at com.example.user.notebook.MainActivity$4.onClick(MainActivity.java:235) at android.view.View.performClick(View.java:4808) at android.view.View$PerformClick.run(View.java:19918) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5608) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

E/Android运行时:致命异常:main 进程: com.example.user.notebook, PID: 4373 java.lang.NumberFormatException: 无效的双精度: " at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseDouble(StringToReal.java:267) at java.lang.Double.parseDouble(Double.java:301) at com.example.user.notebook.MainActivity$3.onClick(MainActivity.java:230) at android.view.View.performClick(View.java:4808) at android.view.View$PerformClick.run(View.java:19918) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5608) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)

您正在尝试通过 results.size() 它是空的。遵循您的代码。

.....
result=db.getResults();
final JSONArray x=new JSONArray();
int i;
for(i=0;i<result.size();i++)
....

这将进入db.getResults。

public ArrayList<Students> getResults()
{
ArrayList<Students> x= new ArrayList<Students>();
Cursor cursor=database.rawQuery("select * from school",null);
if(cursor.getCount()==0)
{
cursor.close();
return x;
}
int lessonsindex=cursor.getColumnIndex("lessons");
int studentindex=cursor.getColumnIndex("student");
int observationsindex=cursor.getColumnIndex("observations");
int gradeindex=cursor.getColumnIndex("grade");
cursor.moveToFirst();
do
{
Students c;
c=new Students(cursor.getString(lessonsindex),
cursor.getString(studentindex),
cursor.getString(observationsindex),
cursor.getDouble(gradeindex));
x.add(c);
}while(cursor.moveToNext());
cursor.close();
return null;
}

如果你看看这种方法...您返回 x,但 x 为空。您没有向 X 添加任何内容。你在最后返回空值。这就是您的问题所在。

PS:请为您的变量使用更好的名称。

ArrayList<Students> x= new ArrayList<Students>();

如果是这样的话,可能会更好,更容易理解......

ArrayList<Students> studentsResults = new ArrayList<Students>();

现在显然你不必这样做,但它使阅读代码更容易......和它的清洁剂...更清洁的方式

您有两个问题,第一个掩盖了第二个问题,第二个问题更明显,因此最初尝试修复导致没有任何效果的问题。

  • 第一个问题是您在onCLick中获取结果数组,然后调用 updateTable() 方法,其中检索到的结果数组不在updateTable方法的范围内。因此,result=db.getResults();转移到updateTable方法上,解决了这个问题。

  • 第二个问题是,如果提取了任何行,getResults将返回 null。

因此,要解决问题并压缩代码,而不是(修复问题2):-

public ArrayList<Students> getResults()
{
ArrayList<Students> x= new ArrayList<Students>();
Cursor cursor=database.rawQuery("select * from school",null);
if(cursor.getCount()==0)
{
cursor.close();
return x;
}
int lessonsindex=cursor.getColumnIndex("lessons");
int studentindex=cursor.getColumnIndex("student");
int observationsindex=cursor.getColumnIndex("observations");
int gradeindex=cursor.getColumnIndex("grade");
cursor.moveToFirst();
do
{
Students c;
c=new Students(cursor.getString(lessonsindex),
cursor.getString(studentindex),
cursor.getString(observationsindex),
cursor.getDouble(gradeindex));
x.add(c);
}while(cursor.moveToNext());
cursor.close();
return null;
}

相反,它可以是:-

public ArrayList<Students> getResults()
{
ArrayList<Students> x= new ArrayList<Students>();
Cursor cursor=database.rawQuery("select * from school",null);
while (cursor.moveToNext()) {
x.add(new Students(
cursor.getString(cursor.getColumnIndex("lessons")),
cursor.getString(cursor.getColumnIndex("student")),
cursor.getString(cursor.getColumnIndex("observations")),
cursor.getDouble(cursor.getColumnIndex("grade"))
));
}
cursor.close();
return x;
}

此外,要在updateTable方法中添加result=db.getResults();(1),例如

public void updateTable()
{
result=db.getResults(); //<<<< ADDED
resultLayout.removeAllViews();
makeTable();
int i;
for(i=0;i<result.size();i++)
{

showRecords.setOnClickListeneronClick方法中删除现在不必要的result=db.getResults();

这既压缩了代码,也不会返回 null,而是返回填充或大小为 0 的 ArrayList。

从而规避java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

最新更新