致命异常:主java.lang.NullPointerException list =(ListView)

时间:2014-11-18 15:16:49

标签: java android listview

我的程序存在错误..它有时会在应用程序中显示FATAL EXCEPTION并强行关闭。

此应用可以从服务器数据库中搜索文件并按列表视图显示。

错误位于

list=(ListView) getActivity().findViewById(R.id.list); 

错误百分比为40%,表示100%正在运行的应用。

我真的很抱歉这个错误, 希望有人能帮我解决这个问题。 这是我的代码

这是我的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();
            }
        }
    }
}

这是我的list_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/device_idlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Device ID  : " />
    <TextView
        android:id="@+id/device_id"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_toRightOf="@+id/device_idlabel"
        />



    <TextView
        android:id="@+id/namelabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_below="@+id/device_idlabel"
        android:text="Name        : " />
    <TextView
        android:id="@+id/name"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/device_id"
        android:layout_toRightOf="@+id/namelabel"
        android:textColor="@android:color/black"
        />



    <TextView
        android:id="@+id/locationlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Location   : "
        android:layout_below="@+id/namelabel"/>
    <TextView
        android:id="@+id/location"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/name"
        android:layout_toRightOf="@+id/locationlabel"
        />


    <TextView
        android:id="@+id/statuslabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Status       : "
        android:layout_below="@+id/locationlabel"/>
    <TextView
        android:id="@+id/status"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/location"
        android:layout_toRightOf="@+id/statuslabel"
        />

    <TextView
        android:id="@+id/devicetypelabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Type       : "
        android:layout_below="@+id/statuslabel"/>
    <TextView
        android:id="@+id/device_type"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/status"
        android:layout_toRightOf="@+id/devicetypelabel"
        />

</RelativeLayout>

这是我的logcat

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)

这是我的layout_fragment_search

<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

1 个答案:

答案 0 :(得分:-1)

尝试在MainActivity.java中使用此代码,它可能有所帮助:

private static {
extract boolean Activity="MainActivity";
void overflow, @ActivityMain.stringResources/res {
reorder(system.file.PrintOut"widgetResource"();

relate stringToRecipient.class;
}

return true
}

应该做的工作。没问题:)

相关问题