サーバー (WAMP) データベースからデータを取得し、単一のリストビューに表示したい。最終的に実装したこれについて学ぶのに長い時間がかかりましたが、logcat にはいくつかの Android ランタイム エラーが表示されます。いくつかのエラーを修正し、修正できないいくつかのエラーを投稿しました。私の .java ファイル:
package com.example.least;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
// url to make request
private static String url = "http://localhost/android_connect/final.php";
// JSON Node names
private static final String TAG_CONTACTS = "opass";
private static final String TAG_ID = "User_ID";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
// Phone number is agin JSON Object
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_ID }, new int[] {
R.id.textView1});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.textView1)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_ID, name);
startActivity(in);
}
});
}
}
logcat 例外:
04-25 19:35:02.353: D/AndroidRuntime(5650): Shutting down VM
04-25 19:35:02.353: W/dalvikvm(5650): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-25 19:35:02.393: E/AndroidRuntime(5650): FATAL EXCEPTION: main
04-25 19:35:02.393: E/AndroidRuntime(5650): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.least/com.example.least.MainActivity}: android.os.NetworkOnMainThreadException
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.os.Looper.loop(Looper.java:137)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-25 19:35:02.393: E/AndroidRuntime(5650): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 19:35:02.393: E/AndroidRuntime(5650): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 19:35:02.393: E/AndroidRuntime(5650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 19:35:02.393: E/AndroidRuntime(5650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 19:35:02.393: E/AndroidRuntime(5650): at dalvik.system.NativeStart.main(Native Method)
04-25 19:35:02.393: E/AndroidRuntime(5650): Caused by: android.os.NetworkOnMainThreadException
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-25 19:35:02.393: E/AndroidRuntime(5650): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-25 19:35:02.393: E/AndroidRuntime(5650): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-25 19:35:02.393: E/AndroidRuntime(5650): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-25 19:35:02.393: E/AndroidRuntime(5650): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-25 19:35:02.393: E/AndroidRuntime(5650): at com.example.least.JSONParser.getJSONFromUrl(JSONParser.java:39)
04-25 19:35:02.393: E/AndroidRuntime(5650): at com.example.least.MainActivity.onCreate(MainActivity.java:45)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.Activity.performCreate(Activity.java:5104)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-25 19:35:02.393: E/AndroidRuntime(5650): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-25 19:35:02.393: E/AndroidRuntime(5650): ... 11 more
04-25 19:35:02.603: D/dalvikvm(5650): GC_CONCURRENT freed 211K, 13% free 2588K/2944K, paused 24ms+30ms, total 226ms
メインの Activity.xml ファイル:
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list">
</ListView>
したがって、私はAndroidが初めてで、これらのタイプのエラーに苦労しています.修正するのを手伝ってください