-2

Hi in my application I'm trying to create the NumberPicker its not working its giving error like.

    06-12 13:07:33.938: E/AndroidRuntime(7521): FATAL EXCEPTION: main
    06-12 13:07:33.938: E/AndroidRuntime(7521): Process: com.politicalmileage.elangovan, PID: 7521
    06-12 13:07:33.938: E/AndroidRuntime(7521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.politicalmileage.elangovan/com.politicalmileage.elangovan.Schedule}: java.lang.NullPointerException
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.os.Handler.dispatchMessage(Handler.java:102)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.os.Looper.loop(Looper.java:136)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at java.lang.reflect.Method.invoke(Method.java:515)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at dalvik.system.NativeStart.main(Native Method)
    06-12 13:07:33.938: E/AndroidRuntime(7521): Caused by: java.lang.NullPointerException
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at com.politicalmileage.elangovan.Schedule.onCreate(Schedule.java:58)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.Activity.performCreate(Activity.java:5231)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    06-12 13:07:33.938: E/AndroidRuntime(7521):     ... 11 more

My XML code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold"
    android:text="TextView" />


<TextView
    android:id="@+id/cost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:text="TextView" />



<Button
    android:id="@+id/bAdd"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_marginLeft="17dp"
    android:layout_toRightOf="@+id/title"
    android:text="+" />

<Button
    android:id="@+id/bSub"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/bAdd"
    android:layout_toRightOf="@+id/tvDisplay"
    android:text="-" />

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/bSub"
    android:layout_alignBottom="@+id/bSub"
    android:layout_toRightOf="@+id/bAdd"
    android:text="1" />

My java code.

   public class Schedule extends ListActivity {

     private ProgressDialog pDialog;
      int counter = 1;
      Button add,sub;
      TextView display;

// URL to get contacts JSON
private static String url = "myurl";

// JSON Node names
private static final String TAG_SCHEDULES = "schedule";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
/*private static final String TAG_CONTENT = "content";*/
private static final String TAG_COST = "cost";
/*private static final String TAG_DATETIME = "date_time";*/

// contacts JSONArray
JSONArray schedules = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> schedule_list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule_list_view);
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        counter++;
        display.setText( "" + counter);
        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
        counter--;
        display.setText( "" + counter);
        }
    });


    schedule_list = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();





    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            /*String content = ((TextView) view.findViewById(R.id.content)).getText().toString();*/
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            /*String datetime = ((TextView) view.findViewById(R.id.datetime)).getText().toString();*/



            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(), ScheduleDetail.class);
            in.putExtra(TAG_TITLE, title);
            in.putExtra(TAG_COST, cost);
            /*in.putExtra(TAG_CONTENT, content);
            in.putExtra(TAG_DATETIME, datetime);*/
            startActivity(in);

        }
    });

    // Calling async task to get json
    new GetSchedule().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetSchedule extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(Schedule.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                schedules = jsonObj.getJSONArray(TAG_SCHEDULES);

                // looping through All Schdules
                for (int i = 0; i < schedules.length(); i++) {
                    JSONObject c = schedules.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String title = c.getString(TAG_TITLE);
                    /*String content = c.getString(TAG_CONTENT);*/
                    String cost = c.getString(TAG_COST);
                    /*String datetime = c.getString(TAG_DATETIME);*/

                    // Phone node is JSON Object

                    // tmp hashmap for Schedule
                    HashMap<String, String> schedule_file = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    schedule_file.put(TAG_ID, id);
                    schedule_file.put(TAG_TITLE, title);
                    /*schedule_file.put(TAG_CONTENT, content);*/
                    schedule_file.put(TAG_COST, cost);
                    /*schedule_file.put(TAG_DATETIME, datetime);*/

                    // adding schedule list
                    schedule_list.add(schedule_file);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */

        ListAdapter adapter = new SimpleAdapter(
                Schedule.this, schedule_list, 
                R.layout.schedule_list_item, 
                new String[] { TAG_TITLE, TAG_COST }, 
                new int[] { R.id.title,R.id.cost });

        setListAdapter(adapter);
      }
     }
   }

I'm trying to create minimum maximum number with some text form my server its not working its showing the null pointer exception error please tell in the above code where i have done wrong how to resolve this issue.

Thanks.

user3374790
  • 227
  • 3
  • 13
  • What is line 58 `Schedule.java`? – Raghunandan Jun 12 '14 at 08:10
  • probably `String title = ((TextView) view.findViewById(R.id.title)).getText().toString();` @Raghunandan – Blackbelt Jun 12 '14 at 08:11
  • post `schedule_list_item.xml`. @blackbelt all the views are in one layout. So yes you are probably right – Raghunandan Jun 12 '14 at 08:12
  • @blackbelt That's not the reason for this NPE as that line is in an anonymous inner subclass and not directly in `onCreate()`. – laalto Jun 12 '14 at 08:34
  • @Raghunandan i am using list view its in another layout inside the list view i am using list items schedule_list_item.xml is above xml – user3374790 Jun 12 '14 at 08:35
  • @user3374790 you need to have the view in `schedule_list_item` the textviews. also you set `schedule_list_view` to the activity so you need to have listview with id `@android:id/list` in `schedule_list_view` as laalto suggested and also have the button in `schedule_list_view` – Raghunandan Jun 12 '14 at 08:52

1 Answers1

1

Your schedule_list_view layout you set with setContentView() does not contain the views you attempt to access with findViewById() and null is returned. Invoking a method on the null causes the NPE.

If schedule_list_view was the content view, you'd get a different exception at getListView() due to missing android:id/list ListView.

laalto
  • 150,114
  • 66
  • 286
  • 303