我正在创建从 MySQL
数据库获取数据的应用程序。几天前,我做了一些重大改变,其中之一是我使用 fragment 和工具栏在它们之间切换,而不是使用多个 Activity 表单。之后我注意到我收到了这个错误:
02/com.example.kurpaest.kurpaest W/JNIHelp﹕ Discarding pending exception (java.lang.NullPointerException) to throw java/lang/IllegalArgumentException
09-12 14:50:53.180 15402-15402/com.example.kurpaest.kurpaest E/MSG﹕ **java.lang.IllegalArgumentException: Empty file name**
当我的前 5 个位置返回 null 时,就会出现此错误。但是当我尝试调试应用程序时,它运行顺利,没有错误或任何类型的问题。
我的服务器位于我的电脑上,我使用 ISP 提供的 IP 地址连接到它。
更有趣的事情是,当使用 WiFi 而不是移动网络时,此错误会更频繁地出现。
我的猜测之一是服务器配置不正确,第二部分是所有选项卡都向服务器发出自己的请求,而不是一个完整的请求。
在 doInBackground() 方法中应用程序突然停止:
@Override
protected String doInBackground(String... params) {
handler.post(new Runnable() {
@Override
public void run() {
gps = new GpsTracker(context);
}
});
HashMap<String,String> param = new HashMap<String,String>();
//param.put("lat", String.valueOf(gps.getLatitude()));
// param.put("long", String.valueOf(gps.getLongitude()));
JSONObject json = jsonParser.makeHttpRequest(url_all_products,"GET",param);
try{
int success = json.getInt(TAG_SUCCESS);
switch(success){
case 1:
products = json.getJSONArray(TAG_LUNCH);
for(int i = 0;i<5;i++){
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
String name =c.getString(TAG_NAME);
Log.d("MSG", name);
String picture ="images/"+c.getString(TAG_PICTURE)+".png";
Log.d("MSG", picture+"wasd");
String cost = c.getString(TAG_COST);
Log.d("MSG", cost);
String lat = c.getString(TAG_LAT);
String longi = c.getString(TAG_LONG);
Log.d("MSG", lat + longi);
String worktime = c.getString(TAG_WORKTIME);
**Log.d("MSG", worktime);**
String distance = calcDist(lat, longi);
lunchList[i][0] = id;
lunchList[i][1] = name;
lunchList[i][2] = picture;
lunchList[i][3] = cost;
lunchList[i][4] = worktime;
lunchList[i][5] = distance ;
gps.stopUsingGPS();
}
break;
case 2:
handler.post(new Runnable() {
@Override
public void run() {
notNetworkDialog.setTitle("Neizdevās izveidot savienojumu!");
notNetworkDialog
.setMessage("Neizdevās izveidot savienojumu ar datu serveri, serveris nav pieejams \n" +
"Mēģināt velreiz?")
.setCancelable(false)
.setPositiveButton("Jā", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadLunches().execute();
}
})
.setNegativeButton("Nē", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Activity)context).finish();
}
});
AlertDialog alertDialog = notNetworkDialog.create();
alertDialog.show();
}
});
break;
case 3:
handler.post(new Runnable() {
@Override
public void run() {
notNetworkDialog.setTitle("Neizdevās izveidot savienojumu!");
notNetworkDialog
.setMessage("Neizdevās izveidot savienojumu ar datu serveri, lūdzu pārliecinaties, ka tālrunis ir savienots ar internetu! \n" +
"Mēģināt velreiz?")
.setCancelable(false)
.setPositiveButton("Jā", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadLunches().execute();
}
})
.setNegativeButton("Nē", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Activity)context).finish();
}
});
AlertDialog alertDialog = notNetworkDialog.create();
alertDialog.show();
}
});
break;
case 0:
handler.post(new Runnable() {
@Override
public void run() {
notNetworkDialog.setTitle("Kļūda");
notNetworkDialog
.setMessage("Kļūme pieprasījuma! \n" +
"Mēģināt velreiz?")
.setCancelable(false)
.setPositiveButton("Jā", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new LoadLunches().execute();
}
})
.setNegativeButton("Nē", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((Activity)context).finish();
}
});
AlertDialog alertDialog = notNetworkDialog.create();
alertDialog.show();
}
});
break;
}
}catch(Exception e){
Log.d("MSG",e.toString());
}
return null;
}
程序一直运行到 Log.d("MSG",worktime)。 然后跳过计算并继续执行以下几行:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pDialog.dismiss();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
lunchAdapter adapter = new lunchAdapter(getActivity(), lunchList, lunchid);
lv.setAdapter(adapter);
}
});
}
最佳答案
在我的案例中,我没有找到此类错误的真正原因。因为我从数据库接收了所有数据,而且没有任何其他文件名称可能错误或丢失。所以我的解决方法非常简单。
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
lunchAdapter adapter = new lunchAdapter(getActivity(), lunchList, lunchid);
lv.setAdapter(adapter);
}
});
我添加了新行
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(lunchlist==null){
new LoadLunches().execute();
}else{
lunchAdapter adapter = new lunchAdapter(getActivity(), lunchList, lunchid);
lv.setAdapter(adapter);
}
});
关于android - 错误:java. lang.IllegalArgumentException:空文件名Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32538659/