致命异常:main java.lang.NullPointerException列表=(ListView)
我的程序有一个bug。。它有时会出现致命异常,并在应用程序中强制关闭 此应用程序可以从服务器数据库中搜索文件并以列表视图显示 错误位于中致命异常:main java.lang.NullPointerException列表=(ListView),java,android,listview,Java,Android,Listview,我的程序有一个bug。。它有时会出现致命异常,并在应用程序中强制关闭 此应用程序可以从服务器数据库中搜索文件并以列表视图显示 错误位于中 list=(ListView) getActivity().findViewById(R.id.list); 从100%工作应用程序中,错误率为40% 我真的对这个错误感到困惑, 希望有人能帮我解决这个问题。 这是我的密码 这是我的MainActivity.java private void handleIntent(Intent intent) {
list=(ListView) getActivity().findViewById(R.id.list);
从100%工作应用程序中,错误率为40%
我真的对这个错误感到困惑,
希望有人能帮我解决这个问题。
这是我的密码
这是我的MainActivity.java
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Fragment fragment = new FragmentSearch();
Bundle data = new Bundle();
data.putString("keyword", query);
fragment.setArguments(data);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
setTitle("Hasil pencarian");
Toast.makeText(MainActivity.this, query, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
FragmentSearch.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_layout_search, container, false);
String keyword = getArguments().getString("keyword");
Log.d("ZZZ", "get keyword : " + keyword);
new JSONParse().execute();
return rootView;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
device_id = (TextView) getActivity().findViewById(R.id.device_id);
name = (TextView) getActivity().findViewById(R.id.name);
location = (TextView) getActivity().findViewById(R.id.location);
status = (TextView) getActivity().findViewById(R.id.status);
device_type = (TextView) getActivity().findViewById(R.id.device_type);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Search Device List...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
String keyword = getArguments().getString("keyword");
Log.d("keyword", "In Search : " + keyword);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("keyword", keyword));
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(url, "POST", params);
Log.d("ZZZ hasil search", json.toString());
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
search = json.getJSONArray(TAG_OS);
for(int i = 0; i < search.length(); i++){
final JSONObject c = search.getJSONObject(i);
// Storing JSON item in a Variable
String device_id = c.getString(TAG_DEVICE_ID);
String name = c.getString(TAG_NAME);
String location = c.getString(TAG_LOCATION);
String status = c.getString(TAG_STATUS);
String device_type = c.getString(TAG_DEVICE_TYPE);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_DEVICE_ID, device_id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, location);
map.put(TAG_STATUS, status);
map.put(TAG_DEVICE_TYPE, device_type);
oslist.add(map);
THIS THE ERROR LINE list=(ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_search,
new String[] {TAG_DEVICE_ID, TAG_NAME,TAG_LOCATION, TAG_STATUS, TAG_DEVICE_TYPE}, new int[] {
R.id.device_id, R.id.name, R.id.location, R.id.status, R.id.device_type});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String click = oslist.get(position).get("device_id");
Log.d("click", "IDP clicked : " + click);
//Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
Fragment fragment = new FragmentScan();
Bundle data = new Bundle();
data.putString("device_id", click);
fragment.setArguments(data);
FragmentManager frgManager = getFragmentManager();
Log.d("CLICK", "Send IDP to Maintenance Add : " + data.getString("device_id"));
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
这是我的布局\u片段\u搜索
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="450dp"
android:layout_above="@+id/getdata"/>
</LinearLayout>
THX BEFORE当异步任务完成时,不能保证片段仍然附加到活动。考虑将演示文稿与网络代码脱钩。你的SraveMyLayOutSox.xml在哪里?请把那个文件寄出去。我想拉阿尔托是对的。我有类似的东西,当时我很快就修好了。您应该测试当您旋转屏幕时,它是否会分离。@MohammadRahchamani我已经附加了fragment_布局_search@Shishi如果我旋转我的屏幕,我会被强制关闭
11-18 14:58:01.318 1516-1516/com.example.blackcustomzier.skripsi W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa61cf908)
11-18 14:58:01.318 1516-1516/com.example.blackcustomzier.skripsi E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.blackcustomzier.skripsi.FragmentSearch$JSONParse.onPostExecute(FragmentSearch.java:119)
at com.example.blackcustomzier.skripsi.FragmentSearch$JSONParse.onPostExecute(FragmentSearch.java:64)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="450dp"
android:layout_above="@+id/getdata"/>
</LinearLayout>