简体   繁体   English

Android Json 项目应用程序崩溃

[英]Android Json Project application crashes

I am having a little trouble with my android development project.我的 android 开发项目有点麻烦。 I have created an application which allows the user to insert/read/update and delete records from a MySQL database.我创建了一个应用程序,它允许用户从 MySQL 数据库插入/读取/更新和删除记录。 This is using JSON.这是使用 JSON。 The application keeps crashing with a fatal exception main?应用程序不断崩溃,出现致命异常 main? Not too sure what it is as none of the pages created are showing any errors.不太确定它是什么,因为创建的页面都没有显示任何错误。 I also have used php to connect to the database and query it.我也使用过php连接到数据库并查询它。 There are 5 java files and 4 xml layout files.有 5 个 java 文件和 4 个 xml 布局文件。 Not sure if you would like me to put them all on here as it might get to be too much so for now I will add them in my public folder of dropbox, if I need to put all of the pages here I will edit the question.不确定您是否希望我将它们全部放在这里,因为它可能会变得太多,所以现在我将它们添加到我的 Dropbox 公共文件夹中,如果我需要将所有页面放在这里,我将编辑问题. https://www.dropbox.com/s/iakgmx7aci78b65/AndroidAssignment.zip?dl=0 https://www.dropbox.com/s/iakgmx7aci78b65/AndroidAssignment.zip?dl=0

UPDATE:更新:

I managed to get rid of most errors but its sending a error saying unable to find explicit activity class ViewAllEmployee for the onClick method;我设法摆脱了大多数错误,但它发送了一个错误,说无法找到 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)

MainActivity.java主活动.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 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>

UPDATE: ViewAllEmployee.java更新: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);
 }
 }

You need to add the activity ViewAllEmployee to your manifest between the Application tags您需要将活动ViewAllEmployee添加到Application标记之间的清单中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM