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

标签 java android sqlite android-layout

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

这是主要 Activity :

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/Zygote: v2 01-24 22:10:49.972 2421-2421/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 01-24 22:14:42.292 2421-2421/com.example.user.notebook E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.notebook, PID: 2421 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 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/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.notebook, PID: 4373 java.lang.NumberFormatException: Invalid double: "" 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 为 null。您没有向 X 添加任何内容。然后...您最后返回 null...这就是您的问题所在。

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

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

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

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

现在显然你不必这样做,但它使阅读代码变得更容易......而且它更干净......方式更干净

关于java - 在 json 中插入/显示/加载崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430712/

相关文章:

java - Android Camera Preview - 只取一部分屏幕数据

java - Gremlin 查询的 Union 方法的使用及其对 Neptune 交易的影响

java - HazelcastInstance bean - 正确的销毁方法是什么?

java - 是否有任何泛型版本的 apache 公共(public)对象池?

java - 当 View 被限制为卡片 View 时,如何修复 View 不离开页面

java - 移动 Canvas 中所有绘制的像素

java - 我如何在 Android 中升级我的数据库

c# - 如何在 Xamarin 中将特定数据更新到 SQLite

android - sqlite 数据库在设备上的位置

java - 如何安装libgdx项目