0

I want to retrieve data from server (WAMP) database and I want to display in a single listview. I took long time to learn about this finally I implemented but the logcat shows several android runtime errors. I fixed some errors and I posted some errors which I cant fix. My .java file:

 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 exceptions:

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

Main Activity.xml file:

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list">
</ListView>

hence I am new to android I am struggling with these type of errors.Please help me to fix

2 Answers2

1

The problem is Caused by: java.lang.RuntimeException: Binary XML file line #1: You must supply a layout_width attribute. and it's caused at line at com.example.least.MainActivity.onCreate(MainActivity.java:36)

So check in your layout file R.layout.activity_main where is missing the layout_width attribute.

BTW, notwithstanding you are new to Android, you should at least read the logcat before posting it on the web because in many cases (and this is one of them) the logcat tells you where the error is and what it's caused by.

Mangusto
  • 1,505
  • 1
  • 13
  • 29
-1

The problem is that you are trying to do the network related operations inside on create . Android only allow network related operations such as getting values from webservice inside thread or AsyncTask. So try to add Asynctask or thread