安卓Json项目应用程序崩溃


Android Json Project application crashes

我的android开发项目遇到了一些小麻烦。我创建了一个应用程序,允许用户从MySQL数据库中插入/读取/更新和删除记录。这是使用JSON。应用程序不断崩溃并出现致命异常main?不太确定是什么,因为创建的页面都没有显示任何错误。我还使用php连接到数据库并查询它。有5个java文件和4个xml布局文件。不确定你是否希望我把它们都放在这里,因为可能会太多,所以现在我会把它们添加到我的dropbox公共文件夹中,如果我需要把所有页面都放在那里,我会编辑这个问题。https://www.dropbox.com/s/iakgmx7aci78b65/AndroidAssignment.zip?dl=0

更新:

我设法消除了大多数错误,但它发送了一个错误,说找不到onClick方法的显式活动类ViewAllEmployee;

Process: com.example.assignment.androidassignment, PID: 2211   android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.assignment.androidassignment/com.example.assignment.androidassignment.ViewAllEmployee}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3424)
at android.app.Activity.startActivityForResult(Activity.java:3385)
at android.app.Activity.startActivity(Activity.java:3627)
at android.app.Activity.startActivity(Activity.java:3595)
at com.example.assignment.androidassignment.MainActivity.onClick(MainActivity.java:93)

主要活动.java

package com.example.assignment.androidassignment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends Activity implements View.OnClickListener{
//Defining views
private EditText editTextLatitude;
private EditText editTextLongitude;
private EditText editTextTimeInserted;
private Button buttonAdd;
private Button buttonView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Initializing views
    editTextLatitude = (EditText) findViewById(R.id.editTextLat);
    editTextLongitude = (EditText) findViewById(R.id.editTextLon);
    editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted);
    buttonAdd = (Button) findViewById(R.id.buttonAdd);
    buttonView = (Button) findViewById(R.id.buttonView);
    //Setting listeners to button
    buttonAdd.setOnClickListener(this);
    buttonView.setOnClickListener(this);
    }

    //Adding an employee
    private void addEmployee(){
    final String lat = editTextLatitude.getText().toString().trim();
    final String lon = editTextLongitude.getText().toString().trim();
    final String timeInserted = editTextTimeInserted.getText().toString().trim();
    class AddEmployee extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(MainActivity.this,"Adding...","Wait...",false,false);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
        }
        @Override
        protected String doInBackground(Void... v) {
            HashMap<String,String> params = new HashMap<>();
            params.put(Config.KEY_LAT,lat);
            params.put(Config.KEY_LON,lon);
            params.put(Config.KEY_TIMEINSERTED,timeInserted);
            RequestHandler rh = new RequestHandler();
            String res = rh.sendPostRequest(Config.URL_ADD, params);
            return res;
        }
        }
        AddEmployee ae = new AddEmployee();
        ae.execute();
 }
@Override
public void onClick(View v) {
    if(v == buttonAdd){
        addEmployee();
    }
    if(v == buttonView){
        Intent intent = new Intent(MainActivity.this,ViewAllEmployee.class);
        startActivity(intent);
        startActivity(new Intent(this,com.example.assignment.androidassignment.ViewAllEmployee.class));
    }
    } 
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment.androidassignment"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:label="@string/app_name" >
<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >
<intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
</application>
</manifest>

更新:ViewAllEmployee.java

package com.example.assignment.assignment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView; 
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ViewAllEmployee extends Activity implements ListView.OnItemClickListener {
private ListView listView;
private String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_all_employee);
    listView = (ListView) findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    getJSON();
}

private void showEmployee() {
    JSONObject jsonObject = null;
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,  String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
        for (int i = 0; i < result.length(); i++) {
            JSONObject jo = result.getJSONObject(i);
            String UserID = jo.getString(Config.TAG_ID);
            String Lat = jo.getString(Config.TAG_LAT);
            HashMap<String, String> employees = new HashMap<>();
            employees.put(Config.TAG_ID, UserID);
            employees.put(Config.TAG_LAT, Lat);
            list.add(employees);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ListAdapter adapter = new SimpleAdapter(
            ViewAllEmployee.this, list, R.layout.list_item,
            new String[]{Config.TAG_ID, Config.TAG_LAT},
            new int[]{R.id.id, R.id.name});
    listView.setAdapter(adapter);
}
private void getJSON() {
    class GetJSON extends AsyncTask<Void, Void, String> {
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(ViewAllEmployee.this, "Fetching Data", "Wait...", false, false);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            JSON_STRING = s;
            showEmployee();
        }
        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(Config.URL_GET_ALL);
            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, ViewEmployee.class);
    HashMap<String, String> map = (HashMap)  parent.getItemAtPosition(position);
    String UserID = map.get(Config.TAG_ID).toString();
    intent.putExtra(Config.EMP_ID, UserID);
    startActivity(intent);
 }
 }

您需要将活动ViewAllEmployee添加到Application标签之间的清单中

<application ...>
    <activity
        android:name=".ViewAllEmployee" />
</application>