2
01-18 02:08:05.271: E/AndroidRuntime(24688): FATAL EXCEPTION: main
01-18 02:08:05.271: E/AndroidRuntime(24688): java.lang.NullPointerException
01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.example.tava.EchangingMessage$LoadData.onPostExecute(EchangingMessage.java:178)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.example.tava.EchangingMessage$LoadData.onPostExecute(EchangingMessage.java:1)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask.finish(AsyncTask.java:602)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.os.Looper.loop(Looper.java:154)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at android.app.ActivityThread.main(ActivityThread.java:4945)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at java.lang.reflect.Method.invoke(Method.java:511)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 02:08:05.271: E/AndroidRuntime(24688):    at dalvik.system.NativeStart.main(Native Method)

And this is my Code Activity Code :

public class EchangingMessage extends Activity implements OnClickListener {

    private ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    ArrayList<HashMap<String, String>> DaftarRS;
    EditText textToSend;

    ArrayList<HashMap<String, String>> arraylist;

    private static String url_daftar_rs = "http://192.168.137.1/www/Android_Login_Secure/detailMessage.php";
    private static String url_daftar_rs1 = "http://192.168.137.1/www/Android_Login_Secure/myFriends.php";

    public static final String TAG_SUCCESS = "success";
    public static final String TAG_DAFTAR_RS = "daftar_rs";
    public static final String TAG_ID_RS = "id_rs";
    public static final String TAG_NAMA_RS = "username";
    public static final String TAG_LINK_IMAGE_RS = "image";
    public static final String TAG_MESSAGE_RS = "messageText";
    public static final String TAG_TIME_RS = "sendAt";
    public static final String TAG_SEEN_RS = "readStatus";

    JSONArray daftar_rs = null;

    ListAdapterUserOnile adapterFirends;

    ImageView showOnline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exchange_message);
arraylist = new ArrayList<HashMap<String, String>>();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.userOnline:
            userOnlineWindow();
            break;

        }

    }

    private void userOnlineWindow() {
        // TODO Auto-generated method stub

        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup, null);
        final PopupWindow popupWindow = new PopupWindow(popupView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
        btnDismiss.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(showOnline, -190, -90);

        new LoadData().execute();

        popupWindow.setFocusable(true);
        popupWindow.update();
    }

    class LoadData extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(EchangingMessage.this);
            pDialog.setMessage("Loading Information...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {

            List<NameValuePair> params1 = new ArrayList<NameValuePair>();

            params1.add(new BasicNameValuePair("me", "52"));

            JSONObject json = jParser.makeHttpRequest(url_daftar_rs1, "POST",
                    params1);

            try {

                int success = json.getInt(TAG_SUCCESS);
                Log.i("success :", "" + success);

                if (success == 1) {

                    daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
                    for (int i = 0; i < daftar_rs.length(); i++) {
                        JSONObject c = daftar_rs.getJSONObject(i);
                        String id_rs = c.getString(TAG_ID_RS);
                        String nama_rs = c.getString(TAG_NAMA_RS);
                        String link_image_rs = c.getString(TAG_LINK_IMAGE_RS);

                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(TAG_ID_RS, id_rs);
                        map.put(TAG_NAMA_RS, nama_rs);
                        map.put(TAG_LINK_IMAGE_RS,
                                "http://192.168.137.1/www/Android_Login_Secure/Images/upload/big/"
                                        + link_image_rs);

                        arraylist.add(map);

                    }
                } else {

                }
            } catch (Exception e) {
                Log.e("Error", "COnnection:" + e.toString());
            }
            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();



        ListView listUserOnline = (ListView) findViewById(R.id.listOfUserOnline);


            adapterFirends = new ListAdapterUserOnile(EchangingMessage.this,
                    arraylist);

            listUserOnline.setAdapter(adapterFirends);

        }
    }

}

I created this app but when I click the top button I got this error and this is the popup window , and this is connect with php

please how to handle this error what should I do ?

enter image description here

and this my EchangeMesssage.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:background="@drawable/backgroudnformain"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#99cc00"
        android:gravity="center"
        >

        <ImageView
            android:id="@+id/userOnline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/userimages" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_write_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/message_bar" >

        <EditText
            android:id="@+id/messageToSend"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="5sp"
            android:layout_weight="1"
            android:background="@drawable/message_field"
            android:hint="Write here"
            android:padding="5sp"
            android:textColor="@color/textFieldColor" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_margin="5sp"
            android:background="@drawable/send_button"
            android:onClick="sendMessage"
            android:paddingBottom="5sp"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"
            android:paddingTop="5sp"
            android:text="Send"
            android:textColor="#FFF" />

    </LinearLayout>

    <ListView
        android:id="@+id/listSend"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_marginTop="40dp"
        android:layout_above="@id/bottom_write_bar"
        android:layout_alignParentTop="true"
        android:cacheColorHint="#00000000"
        android:divider="#00000000"
        android:stackFromBottom="true"
        android:clickable="false"
        android:scrollbars="none"
         android:transcriptMode="alwaysScroll"
        android:listSelector="#00000000" >
    </ListView>


        <TextView
            android:id="@+id/you_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone" />

        <TextView
            android:id="@+id/me_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone" />


</RelativeLayout>

and this is my popup.xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/windowwww"
    android:orientation="vertical" >

    <Button
        android:id="@+id/dismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="240dp"
        android:layout_marginTop="60dp"
        android:text="EXIT" />

    <ListView
        android:id="@+id/listOfUserOnline"
        android:layout_width="match_parent"
        android:layout_height="330dp"
        android:layout_marginTop="30dp" >
    </ListView>

</LinearLayout>

And this is my ListAdapter class :

public class ListAdapterUserOnile extends BaseAdapter {

    public String POST_TEXT;
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader;
    public final static String you_id=null;

    int count = 0; 

    public ListAdapterUserOnile(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.item_list_friends, null);
        }

        TextView name_rs = (TextView) vi.findViewById(R.id.myFriendUsername);


        ImageView thumb_image = (ImageView) vi
                .findViewById(R.id.myFriendImage);


        TextView status = (TextView) vi.findViewById(R.id.status);

        ImageView on=(ImageView) vi.findViewById(R.id.online);
        ImageView off=(ImageView) vi.findViewById(R.id.offline);

        HashMap<String, String> daftar_rs = new HashMap<String, String>();
        daftar_rs = data.get(position);
        final int getPosition = position;
        name_rs.setText(daftar_rs.get(EchangingMessage.TAG_NAMA_RS));







        Log.e("IMage", daftar_rs.get(EchangingMessage.TAG_LINK_IMAGE_RS));

        imageLoader.DisplayImage(daftar_rs.get(EchangingMessage.TAG_LINK_IMAGE_RS),
                thumb_image);

        if (status.getText().toString().equals("1")) {

            off.setVisibility(View.INVISIBLE);
            on.setVisibility(View.VISIBLE);
            status.setVisibility(View.INVISIBLE);

        }
        else
        { 
            off.setVisibility(View.VISIBLE);
            on.setVisibility(View.INVISIBLE);
            status.setVisibility(View.INVISIBLE);
        }




        vi.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        activity,
                        "  You click"
                                + data.get(getPosition)
                                        .get(MyFriends.TAG_ID_RS).toString(),
                        Toast.LENGTH_SHORT).show();

                Intent i=new Intent(activity,EchangingMessage.class);
                i.putExtra(you_id, data.get(getPosition)
                                        .get(MyFriends.TAG_ID_RS).toString());
                activity.startActivity(i);


            }
        });

        return vi;

    }

}
Shanaz K
  • 678
  • 2
  • 13
  • 28

2 Answers2

4

When you use setContentView(R.layout.exchange_message), the layout R.layout.exchange_message will be inflated and associated to your activity. If you want to retrieve a View from that layout, you just call findViewById(R.id.MyId);.

But if your View is not inside the layout, you will have a NPException.

To retrieve views from another layout, you have to inflate it first, and then call myView.findViewById(R.id.MyId).

For instance, according to your case, if you want to use the ListView from the popup.xml layout file :

    View popupView = layoutInflater.inflate(R.layout.popup, null);
    ListView list = popupView.findViewById(R.id.listOfUserOnline);
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
2

You need to learn to read the Log to solve your errors like the NPE. java.lang.NullPointerException at com.example.tava.EchangingMessage$LoadData.onPostExecute*(EchangingMessage.java:178)*

Your error is most likely on this row. To show Line number in eclipse right click on the edge to the left and choose "Show Line Number" Also the Key command Ctrl+L will let you jump to the specific line.

Invoke findViewById on your popupWindow instead. Extract the variable to the class level or pass it to the AsyncTask so it can be referenced.

popupWindow.findViewById();
Nicklas Gnejs Eriksson
  • 3,395
  • 2
  • 21
  • 19
  • listUserOnline.setAdapter(adapterFirends); this is my error point – Shanaz K Jan 17 '14 at 23:22
  • You gave him/her a good advice, but you haven't helped him/her too much. He/She gave the line number and code, what should be the cause of that NPE ? answer to this may help him/her better ... – S.Thiongane Jan 17 '14 at 23:25
  • Then your findViewById(R.id.listOfUserOnline) failed, are you sure the id is correct? Is it the activity that has this view? If the view belongs to another component you have to invoke .findViewById on that instead. For example if it belongs to a dialog you need to invoke it on the dialog, thus making it dialog.findViewById() – Nicklas Gnejs Eriksson Jan 17 '14 at 23:26