单击未在android中插入数据库的保存按钮数据

时间:2014-09-22 10:23:51

标签: php android mysql

嗨在我的应用程序中我正在显示学生名称并且滚动否和复选框名称已存在。现在学生姓名和滚动来自数据库现在如果我单击保存按钮我想在另一个表中插入所有记录。如果我单击“保存”按钮,我希望将所有数据保存到数据库中。现在,如果单击“保存按钮”,则会显示错误。

任何人都可以帮助我。

Attend.java

public class Attend extends Activity implements OnClickListener {

    String data = "";
    TableLayout tl;
    TableRow tr;
    TextView studentName;
    EditText studentname,rollno;
    CheckBox present;
    String[] class_name, section_name;
    String[] words;
    String name;
    String roll;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tl = (TableLayout) findViewById(R.id.maintable);

         save = (Button)findViewById(R.id.save); 

         save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //save.setText("Record Saved Sucessfully");
                name = studentname.getText().toString();
                roll =rollno.getText().toString();
                data = DatabaseUtility.executeQueryPhp("save","");
            }


        });

        String class_id = getIntent().getStringExtra("class_id");
        String section_id = getIntent().getStringExtra("section_id");
        Toast.makeText(
                this,
                "Result : " + "\nclassName : " + class_id + "\nSectionName : "
                        + section_id,

                Toast.LENGTH_SHORT).show();

        final String queryString = "class_id=" + class_id + "&section_id="
                + section_id;
        /*String result = DatabaseUtility.executeQueryPhp("getStudent",
                queryString);*/

        new Thread(new Runnable() {
            public void run() {
                data = DatabaseUtility.executeQueryPhp("getStudent",queryString);
                System.out.println(data);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        ArrayList<StudentDetails> users = parseJSON(data);
                        addData(users);
                    }
                });

            }
        }).start();

    }

    public ArrayList<StudentDetails> parseJSON(String result) {
        ArrayList<StudentDetails> users = new ArrayList<StudentDetails>();
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                StudentDetails user = new StudentDetails();
                // user.setId(json_data.getInt("class_name"));
                // user.setClass_id(json_data.getInt("section_name"));
                // user.setSection_id(json_data.getInt("section_id"));
                user.setStudentname(json_data.getString("first_name"));
                user.setRollno(json_data.getString("roll_no"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
        return users;
    }

    void addHeader() {

        tr = new TableRow(this);

        studentName = new TextView(this);


        studentName.setText("StudentName");

        studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        studentName.setPadding(5, 5, 5, 5);
        studentName.setBackgroundColor(Color.RED);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 5, 5, 5);

        Ll.addView(studentName, params);
        tr.addView((View) Ll);

        TextView rollNumber = new TextView(this);
        rollNumber.setText("RollNo");
        rollNumber.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        rollNumber.setPadding(5, 5, 5, 5);
        rollNumber.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);

        Ll.addView(rollNumber, params);
        tr.addView((View) Ll);
        TextView Present = new TextView(this);
        Present.setText("Present");
        Present.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        Present.setPadding(5, 5, 5, 5);
        Present.setBackgroundColor(Color.RED);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 5, 5);

        Ll.addView(Present, params);
        tr.addView((View) Ll);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));



    }

    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<StudentDetails> users) {

        addHeader();

        for (Iterator i = users.iterator(); i.hasNext();) {

            StudentDetails p = (StudentDetails) i.next();

            /** Create a TableRow dynamically **/
            tr = new TableRow(this);

            /** Creating a TextView to add to the row **/
            studentName = new EditText(this);
            studentName.setText(p.getStudentname());
            studentName.setId(p.getId());
            studentName.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            studentName.setPadding(5, 5, 5, 5);

            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);

            Ll.addView(studentName, params);
            tr.addView((View) Ll);

            EditText rollo = new EditText(this);
            rollo.setText(p.getRollno());
            rollo.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            rollo.setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(rollo, params);
            tr.addView((View) Ll);
            CheckBox present = new CheckBox(this);

            present.setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            present.setPadding(5, 5, 5, 5);

            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);

            Ll.addView(present, params);
            tr.addView((View) Ll);

            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

DatabaseUtility.java

public class DatabaseUtility {

    public static String executeQueryPhp(String phpScript,String queryString) {
        InputStream is = null;
        String line = null;
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            String url = "";
            if(queryString!=""){
                url= "http://10.0.2.2/android_connect/"+phpScript+".php?"+queryString;  
            }else{
                url = "http://10.0.2.2/android_connect/"+phpScript+".php"; 
            }
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            Log.e("Fail 1", "3");

            HttpEntity entity = response.getEntity();
            Log.e("Fail 1", "4");

            is = entity.getContent();
            Log.e("Pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            /*
             * Toast.makeText(getApplicationContext(), "Invalid IP Address",
             * Toast.LENGTH_LONG).show();
             */
            /* finish(); */
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }
        return result;
    }

logcat的

    09-22 10:40:42.193: E/Fail 1(8270): 3
09-22 10:40:42.193: E/Fail 1(8270): 4
09-22 10:40:42.193: E/Pass 1(8270): connection success 
09-22 10:40:42.443: I/Choreographer(8270): Skipped 51 frames!  The application may be doing too much work on its main thread.
09-22 10:40:42.513: D/gralloc_goldfish(8270): Emulator without GPU emulation detected.
09-22 10:40:42.613: E/Position new(8270): Nursery
09-22 10:40:47.386: D/dalvikvm(8270): GC_FOR_ALLOC freed 186K, 11% free 2958K/3312K, paused 199ms, total 209ms
09-22 10:40:47.592: D/dalvikvm(8270): GC_FOR_ALLOC freed 34K, 10% free 3236K/3588K, paused 32ms, total 35ms
09-22 10:40:47.943: I/Choreographer(8270): Skipped 135 frames!  The application may be doing too much work on its main thread.
09-22 10:40:48.112: I/Choreographer(8270): Skipped 41 frames!  The application may be doing too much work on its main thread.
09-22 10:40:49.053: D/dalvikvm(8270): GREF has increased to 201
09-22 10:40:49.283: E/Position new(8270): Play Group
09-22 10:40:51.913: I/Choreographer(8270): Skipped 201 frames!  The application may be doing too much work on its main thread.
09-22 10:40:52.133: I/Choreographer(8270): Skipped 56 frames!  The application may be doing too much work on its main thread.
09-22 10:40:52.716: I/Choreographer(8270): Skipped 44 frames!  The application may be doing too much work on its main thread.
09-22 10:40:53.153: I/Choreographer(8270): Skipped 68 frames!  The application may be doing too much work on its main thread.
09-22 10:40:54.093: I/Choreographer(8270): Skipped 54 frames!  The application may be doing too much work on its main thread.
09-22 10:40:55.303: I/Choreographer(8270): Skipped 424 frames!  The application may be doing too much work on its main thread.
09-22 10:40:55.383: D/dalvikvm(8270): GC_CONCURRENT freed 73K, 6% free 3571K/3788K, paused 5ms+33ms, total 324ms
09-22 10:40:55.832: E/Fail 1(8270): 3
09-22 10:40:55.832: E/Fail 1(8270): 4
09-22 10:40:55.832: E/Pass 1(8270): connection success 
09-22 10:40:55.903: I/System.out(8270): [{"first_name":"Md","roll_no":"0"},{"first_name":"Sana","roll_no":"1"},{"first_name":"Tanweer","roll_no":"1"},{"first_name":"zamal","roll_no":"0"}]
09-22 10:41:00.331: I/Choreographer(8270): Skipped 2731 frames!  The application may be doing too much work on its main thread.
09-22 10:41:02.053: I/Choreographer(8270): Skipped 57 frames!  The application may be doing too much work on its main thread.
09-22 10:41:02.273: I/Choreographer(8270): Skipped 45 frames!  The application may be doing too much work on its main thread.
09-22 10:41:05.502: D/AndroidRuntime(8270): Shutting down VM
09-22 10:41:05.502: W/dalvikvm(8270): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-22 10:41:05.542: E/AndroidRuntime(8270): FATAL EXCEPTION: main
09-22 10:41:05.542: E/AndroidRuntime(8270): java.lang.NullPointerException
09-22 10:41:05.542: E/AndroidRuntime(8270):     at com.rushda.attendance.Attend$1.onClick(Attend.java:68)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.view.View.performClick(View.java:4204)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.view.View$PerformClick.run(View.java:17355)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.os.Handler.handleCallback(Handler.java:725)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.os.Looper.loop(Looper.java:137)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-22 10:41:05.542: E/AndroidRuntime(8270):     at dalvik.system.NativeStart.main(Native Method)
09-22 10:41:10.093: I/Process(8270): Sending signal. PID: 8270 SIG: 9
09-22 10:41:11.653: D/dalvikvm(8301): GC_CONCURRENT freed 75K, 8% free 2745K/2968K, paused 7ms+22ms, total 84ms
09-22 10:41:11.833: D/dalvikvm(8301): GC_CONCURRENT freed 20K, 6% free 3140K/3308K, paused 6ms+22ms, total 63ms
09-22 10:41:12.094: E/Fail 1(8301): 3
09-22 10:41:12.094: E/Fail 1(8301): 4
09-22 10:41:12.094: E/Pass 1(8301): connection success 
09-22 10:41:12.253: E/Position new(8301): Play Group
09-22 10:41:12.393: E/Position new(8301): Play Group
09-22 10:41:12.433: D/gralloc_goldfish(8301): Emulator without GPU emulation detected.
09-22 10:41:13.973: D/dalvikvm(8301): GREF has increased to 201
09-22 10:41:14.073: I/Choreographer(8301): Skipped 45 frames!  The application may be doing too much work on its main thread.
09-22 10:41:14.893: I/Choreographer(8301): Skipped 246 frames!  The application may be doing too much work on its main thread.
09-22 10:41:15.304: I/Choreographer(8301): Skipped 116 frames!  The application may be doing too much work on its main thread.
09-22 10:41:15.345: D/dalvikvm(8301): GC_CONCURRENT freed 203K, 10% free 3331K/3680K, paused 5ms+98ms, total 334ms
09-22 10:41:15.693: E/Fail 1(8301): 3
09-22 10:41:15.693: E/Fail 1(8301): 4
09-22 10:41:15.703: E/Pass 1(8301): connection success 
09-22 10:41:15.813: I/System.out(8301): [{"first_name":"Md","roll_no":"0"},{"first_name":"Sana","roll_no":"1"},{"first_name":"Tanweer","roll_no":"1"},{"first_name":"zamal","roll_no":"0"}]
09-22 10:41:17.433: I/Choreographer(8301): Skipped 984 frames!  The application may be doing too much work on its main thread.
09-22 10:41:19.063: I/Choreographer(8301): Skipped 85 frames!  The application may be doing too much work on its main thread.
09-22 10:41:19.873: I/Choreographer(8301): Skipped 391 frames!  The application may be doing too much work on its main thread.
09-22 10:41:24.513: I/Choreographer(8301): Skipped 89 frames!  The application may be doing too much work on its main thread.
09-22 10:41:25.055: E/SpannableStringBuilder(8301): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-22 10:41:25.104: E/SpannableStringBuilder(8301): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-22 10:41:25.212: I/Choreographer(8301): Skipped 71 frames!  The application may be doing too much work on its main thread.
09-22 10:41:25.632: I/Choreographer(8301): Skipped 253 frames!  The application may be doing too much work on its main thread.
09-22 10:41:25.777: D/dalvikvm(8301): GC_CONCURRENT freed 150K, 8% free 3576K/3872K, paused 28ms+40ms, total 213ms
09-22 10:41:26.220: I/Choreographer(8301): Skipped 100 frames!  The application may be doing too much work on its main thread.
09-22 10:41:27.012: W/InputEventReceiver(8301): Attempted to finish an input event but the input event receiver has already been disposed.
09-22 10:41:27.012: W/InputEventReceiver(8301): Attempted to finish an input event but the input event receiver has already been disposed.
09-22 10:41:27.012: W/InputEventReceiver(8301): Attempted to finish an input event but the input event receiver has already been disposed.
09-22 10:41:27.033: I/Choreographer(8301): Skipped 94 frames!  The application may be doing too much work on its main thread.
09-22 10:41:27.267: I/Choreographer(8301): Skipped 149 frames!  The application may be doing too much work on its main thread.
09-22 10:41:27.422: I/Choreographer(8301): Skipped 88 frames!  The application may be doing too much work on its main thread.
09-22 10:41:29.473: D/AndroidRuntime(8301): Shutting down VM
09-22 10:41:29.473: W/dalvikvm(8301): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-22 10:41:29.503: E/AndroidRuntime(8301): FATAL EXCEPTION: main
09-22 10:41:29.503: E/AndroidRuntime(8301): java.lang.NullPointerException
09-22 10:41:29.503: E/AndroidRuntime(8301):     at com.rushda.attendance.Attend$1.onClick(Attend.java:68)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.view.View.performClick(View.java:4204)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.view.View$PerformClick.run(View.java:17355)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.os.Handler.handleCallback(Handler.java:725)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.os.Looper.loop(Looper.java:137)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-22 10:41:29.503: E/AndroidRuntime(8301):     at dalvik.system.NativeStart.main(Native Method)
09-22 10:41:36.393: I/Process(8301): Sending signal. PID: 8301 SIG: 9
09-22 10:41:37.163: E/Trace(8330): error opening trace file: No such file or directory (2)
09-22 10:41:37.632: D/dalvikvm(8330): GC_CONCURRENT freed 80K, 8% free 2743K/2972K, paused 11ms+5ms, total 101ms
09-22 10:41:37.823: D/dalvikvm(8330): GC_CONCURRENT freed 19K, 6% free 3140K/3308K, paused 5ms+17ms, total 69ms
09-22 10:41:38.083: E/Fail 1(8330): 3
09-22 10:41:38.083: E/Fail 1(8330): 4
09-22 10:41:38.083: E/Pass 1(8330): connection success 
09-22 10:41:38.203: E/Position new(8330): Play Group
09-22 10:41:38.333: E/Position new(8330): Play Group
09-22 10:41:38.333: I/Choreographer(8330): Skipped 30 frames!  The application may be doing too much work on its main thread.
09-22 10:41:38.463: D/gralloc_goldfish(8330): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:0)

你错过了这些:

studentname  = (EditText) findViewById(R.id.studentname );
rollno = (EditText) findViewById(R.id.rollno );
  • 假设studentname和rollno是布局XML文件中EditTexts的ID

在从他们那里读取getText()之前将它们添加到onClick(View v)