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)
這是我的代碼活動代碼:如何在android中處理NullPointerException?
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);
}
}
}
我創造了這個應用程序,但是當我點擊頂部按鈕我得到這個錯誤 ,這是彈出窗口,這是連接用PHP
請如何處理這個錯誤我該怎麼辦?
,這我EchangeMesssage.xml文件
<?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>
,這是我popup.xml文件:
<?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>
這是我ListAdapter類:
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;
}
}
哪條線對應的行號178? – nKn
你確定變量listUserOnline不是null嗎?如果您的ID在R.java中找不到給定的ID,findViewByID可以返回null。 – Prmths
yess你是對的我刪除了一些代碼 現在看這個 –