当我尝试使用以下代码调用方法 onclick 时,我的应用程序崩溃了。
完整活动:
public class UserCom extends ListActivity implements OnClickListener {
private ProgressDialog m_ProgressDialog = null;
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;
private Runnable viewOrders;
private Thread t;
String valcom;
String valnm;
EditText editText1;
EditText editText2;
URL url;
InputStream is;
HttpURLConnection h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_com);
View subBTN = findViewById(R.id.button1);
subBTN.setOnClickListener(this);
m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(this, R.layout.listlook, m_orders);
setListAdapter(this.m_adapter);
viewOrders = new Runnable(){
@Override
public void run() {
getOrders();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(UserCom.this,
"Please wait...", "Retrieving data ...", true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_com, menu);
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
sendTextToDB();
break;
}
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONArray json = new JSONArray(jsonText);
return json;
} finally {
is.close();
}
}
private void getOrders(){
m_orders = new ArrayList<Order>();
JSONArray json;
JSONObject jo;
try{
json = readJsonFromUrl("http://10.0.2.2/HB/hikebuddy.php?function=usercomm");
jo = (JSONObject) json.get(0);
Order o1 = new Order();
o1.setOrderName(jo.get("comment").toString());
o1.setOrderStatus(jo.get("name").toString());
// Order o2 = new Order();
// o2.setOrderName("SF Advertisement");
// o2.setOrderStatus("Completed");
m_orders.add(o1);
// m_orders.add(o2);
Thread.sleep(1000);
Log.i("ARRAY", ""+ m_orders.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
private class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listlook, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.txtl1);
TextView bt = (TextView) v.findViewById(R.id.txtl2);
if (tt != null) {
tt.setText(o.getOrderName()); }
if(bt != null){
bt.setText(o.getOrderStatus());
}
}
return v;
}
}
public void sendTextToDB() {
valcom = editText1.getText().toString();
valnm = editText2.getText().toString();
t = new Thread() {
public void run() {
try {
url = new URL("http://10.0.2.2/HB/hikebuddy.php?function=setcomm&comment="+valcom+"&name="+valnm);
h = (HttpURLConnection)url.openConnection();
if( h.getResponseCode() == HttpURLConnection.HTTP_OK){
is = h.getInputStream();
// do something with the data here
}else{
is = h.getErrorStream();
}
h.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Test", "CONNECTION FAILED 1");
}
}
};
t.start();
}
}
我该如何解决?
Logcat 错误:
10-21 16:56:00.198: E/AndroidRuntime(982): 致命异常: main 10-21 16:56:00.198: E/AndroidRuntime(982): java.lang.NullPointerException 10-21 16:56:00.198: E/AndroidRuntime(982): 在 com.example.hikebuddy.UserCom.sendTextToDB(UserCom.java:193) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 com.example.hikebuddy.UserCom.onClick (UserCom.java:92) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 android.view.View.performClick(View.java:2485) 10-21 16:56:00.198: E/AndroidRuntime (982): 在 android.view.View$PerformClick.run(View.java:9080) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 android.os.Handler.handleCallback(Handler.java: 587) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 android.os.Handler.dispatchMessage(Handler.java:92) 10-21 16:56:00.198: E/AndroidRuntime(982): 在android.os.Looper.loop(Looper.java:123) 10-21 16:56:00.198:E/AndroidRuntime(982): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 java.lang.reflect.Method.invokeNative(Native方法) 10-21 16:56:00.198: E/AndroidRuntime(982): at java.lang.reflect.Method.invoke(Method.java:507) 10-21 16:56:00.198: E/AndroidRuntime(982) : 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:597) 10-21 16:56:00.198: E/AndroidRuntime(982): at dalvik.system.NativeStart.main(Native Method)调用(Method.java:507)10-21 16:56:00.198:E/AndroidRuntime(982):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)10-21 16: 56:00.198: E/AndroidRuntime(982): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 dalvik.system .NativeStart.main(本机方法)调用(Method.java:507)10-21 16:56:00.198:E/AndroidRuntime(982):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)10-21 16: 56:00.198: E/AndroidRuntime(982): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-21 16:56:00.198: E/AndroidRuntime(982): 在 dalvik.system .NativeStart.main(本机方法)