ClassCastException:android.widget.TextView无法强制转换为android.widget.ListView

我不知道为什么我得到这个类强制转换exception。 我已经做了几次[项目 – >清洁],仍然没有工作。

有人请帮帮我。

谢谢。

这是ScheduleFragment.java

public class ScheduleFragment extends Fragment { public static final String PREFS_NAME="PreferencesValue"; public ProgressDialog pDialog; private ListView myListView; // Creating JSON Parser object JSONParser jParser = new JSONParser(); ArrayList<HashMap> subjectList; // url to get all subjects list private static String url_all_subjects = "http://192.168.1.12/android_project/get_subjects.php"; // JSON Node names private static final String TAG_SUCCESS = "success"; private static final String TAG_STUDENT = "student"; private static final String TAG_MATRIX_ID = "matrix_id"; private static final String TAG_NAME = "name"; // subject JSONArray JSONArray student = null; public ScheduleFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_schedule, container, false); //Bring the value from login page TextView tvmatrix = (TextView)rootView.findViewById(R.id.textViewMatrix); SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0); tvmatrix.setText(settings.getString("matrix", "A13123")); //------------------------------------------------------------------------- //------------------------------------CREATING A LISTVIEW----------------------- // Loading subject in Background Thread new LoadAllSubject().execute(); // Hashmap for ListView subjectList = new ArrayList<HashMap>(); myListView = (ListView) rootView.findViewById(R.id.textViewMatrix); // on selecting single subject myListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // getting values from selected ListItem String matrix_id = ((TextView) view.findViewById(R.id.textViewMatrix)).getText() .toString(); // Starting new intent Intent in = new Intent(getActivity().getApplicationContext(), SingleSubject.class); // sending matrix id to next activity in.putExtra(TAG_MATRIX_ID, matrix_id); // starting new activity and expecting some response back startActivityForResult(in, 100); } }); return rootView; } /** * Background Async Task to Load all product by making HTTP Request * */ class LoadAllSubject extends AsyncTask { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = ProgressDialog.show(ScheduleFragment.this.getActivity(), "Progress", "Loading subjects. Please wait...", false); pDialog.setIndeterminate(false); pDialog.setCancelable(false); } /** * getting All products from url * */ protected String doInBackground(String... args) { // Building Parameters List params = new ArrayList(); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(url_all_subjects, "GET", params); // Check your log cat for JSON reponse Log.d("All Subjects: ", json.toString()); try { // Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); if (success == 1) { // subject found // Getting Array of Subject student = json.getJSONArray(TAG_STUDENT); // looping through All Subjects for (int i = 0; i < student.length(); i++) { JSONObject c = student.getJSONObject(i); // Storing each json item in variable String matrix_id = c.getString(TAG_MATRIX_ID); String name = c.getString(TAG_NAME); // creating new HashMap HashMap map = new HashMap(); // adding each child node to HashMap key => value map.put(TAG_MATRIX_ID, matrix_id); map.put(TAG_NAME, name); // adding HashList to ArrayList subjectList.add(map); } } else { // no subjects found } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { super.onPostExecute(file_url); // dismiss the dialog after getting all products pDialog.dismiss(); // updating UI from Background Thread ListAdapter adapter = new SimpleAdapter( getActivity(), subjectList, R.layout.all_subject, new String[] { TAG_MATRIX_ID, TAG_NAME}, new int[] { R.id.matrix_id, R.id.name }); // updating listview myListView.setAdapter(adapter); } } } 

这是all_subject.xml

     

这是fragment_schedule.xml

    

这是错误日志。

  03-25 12:14:06.177: W/dalvikvm(1129): threadid=1: thread exiting with uncaught exception (group=0x40d09930) 03-25 12:14:06.271: E/AndroidRuntime(1129): FATAL EXCEPTION: main 03-25 12:14:06.271: E/AndroidRuntime(1129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ultra.esc/com.ultra.esc.HomeActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.os.Handler.dispatchMessage(Handler.java:99) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.os.Looper.loop(Looper.java:137) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread.main(ActivityThread.java:5039) 03-25 12:14:06.271: E/AndroidRuntime(1129): at java.lang.reflect.Method.invokeNative(Native Method) 03-25 12:14:06.271: E/AndroidRuntime(1129): at java.lang.reflect.Method.invoke(Method.java:511) 03-25 12:14:06.271: E/AndroidRuntime(1129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-25 12:14:06.271: E/AndroidRuntime(1129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-25 12:14:06.271: E/AndroidRuntime(1129): at dalvik.system.NativeStart.main(Native Method) 03-25 12:14:06.271: E/AndroidRuntime(1129): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ListView 03-25 12:14:06.271: E/AndroidRuntime(1129): at com.ultra.esc.ScheduleFragment.onCreateView(ScheduleFragment.java:88) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.Fragment.performCreateView(Fragment.java:1695) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.BackStackRecord.run(BackStackRecord.java:682) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.Activity.performStart(Activity.java:5113) 03-25 12:14:06.271: E/AndroidRuntime(1129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 03-25 12:14:06.271: E/AndroidRuntime(1129): ... 11 more 

你有这个

  myListView = (ListView) rootView.findViewById(R.id.textViewMatrix); 

可能是id textViewMatrix是一个TextView

确认您已使用相同的ID初始化textview

 TextView tvmatrix = (TextView)rootView.findViewById(R.id.textViewMatrix); // see the id textViewMatrix its a textview and you use the same to initialize listview 

您需要在fragment_schedule.xml拥有ListView

  

  

接着

 myListView = (ListView) rootView.findViewById(R.id.list); 

你错了

  myListView = (ListView) rootView.findViewById(R.id.textViewMatrix); 

textViewMatrix是TextView id而不是ListView id,并且还发布了fragment_schedule.xml文件

并改变这一点

  

  

并在你的片段中使用

 myListView = (ListView) rootView.findViewById(R.id.list);