2014-04-26 3 views
-2

런타임에 오류가 발생합니다. java.lang.classcastexception com.android.homework.AllAppsFragment를 android.app.activity로 캐스팅 할 수 없습니다. 이 코드에 무슨 문제가 있습니까?java.lang.classcastexception 조각을 android.app.activity로 캐스팅 할 수 없습니다.

04-26 08 : 42 : 02.065 : E/AndroidRuntime (1755) java.lang.RuntimeException가 : 활성 ComponentInfo {com.android.homework/com.andorid.homework.AllAppsFragment}를 인스턴스화 할 수 없습니다 : java.lang.ClassCastException가 : com.andorid.homework.AllAppsFragment 는 HostActivity

public class AllAppsFragment extends ListFragment { 
    private PackageManager packageManager = null; 
    private List<ApplicationInfo> applist = null; 
    private ApplicationAdapter listadaptor = null; 
    private HashMap<ApplicationInfo, Float> appRating = null; 
    private SharedPreferences sh = null; 
    private SharedPreferences.Editor preferencesEditor = null; 
    public static final String MY_PREFERENCES = "myPreferences"; 

    private OnItemSelectedListener listener; 

    public interface OnItemSelectedListener { 
     public void onRssItemSelected(String link); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     if (activity instanceof OnItemSelectedListener) { 
      listener = (OnItemSelectedListener) activity; 
     } else { 
      throw new ClassCastException(activity.toString() 
        + " must implemenet MyListFragment.OnItemSelectedListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     listener = null; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // setContentView(R.layout.activity_main); 

     packageManager = getActivity().getBaseContext().getPackageManager(); 
     new LoadApplications().execute(); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.activity_main, null); 
    } 

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     // MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     inflater.inflate(R.menu.menu, menu); 
     super.onCreateOptionsMenu(menu, inflater); 
     // return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     boolean result = true; 

     switch (item.getItemId()) { 
     case R.id.menu_about: { 
      displayAboutDialog(); 

      break; 
     } 
     default: { 
      result = super.onOptionsItemSelected(item); 

      break; 
     } 
     } 

     return result; 
    } 

    private void displayAboutDialog() { 
     final AlertDialog.Builder builder = new AlertDialog.Builder(
       getActivity().getBaseContext()); 
     builder.setTitle(getString(R.string.about_title)); 

     builder.setItems(new CharSequence[] { getString(R.string.sort_lex), 
       getString(R.string.sortuj_lex_desc), 
       getString(R.string.sort_ranked), 
       getString(R.string.sort_ranked_desc) }, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // The 'which' argument contains the index position 
         // of the selected item 
         sh = getActivity().getBaseContext() 
           .getSharedPreferences(MY_PREFERENCES, 
             Context.MODE_PRIVATE); 
         preferencesEditor = sh.edit(); 
         switch (which) { 
         case 0: 
          listadaptor.sortLex(); 
          preferencesEditor.putInt("sort_type", 1); 
          preferencesEditor.commit(); 
          dialog.cancel(); 
          break; 
         case 1: 
          listadaptor.sortLexDesc(); 
          preferencesEditor.putInt("sort_type", 2); 
          preferencesEditor.commit(); 
          dialog.cancel(); 
          break; 
         case 2: 
          listadaptor.sortRating(); 
          preferencesEditor.putInt("sort_type", 3); 
          preferencesEditor.commit(); 
          dialog.cancel(); 
          break; 
         case 3: 
          listadaptor.sortRaingDesc(); 
          preferencesEditor.putInt("sort_type", 4); 
          preferencesEditor.commit(); 
          dialog.cancel(); 
          break; 
         } 
        } 
       }); 

     builder.create().show(); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     ApplicationInfo app = applist.get(position); 
     try { 
      Intent intent = packageManager 
        .getLaunchIntentForPackage(app.packageName); 
      if (null != intent) { 
       startActivity(intent); 
      } 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(getActivity().getBaseContext(), e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(getActivity().getBaseContext(), e.getMessage(), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    private List<ApplicationInfo> checkForLaunchIntent(
      List<ApplicationInfo> list) { 
     ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>(); 
     for (ApplicationInfo info : list) { 
      try { 
       if (null != packageManager 
         .getLaunchIntentForPackage(info.packageName)) { 
        applist.add(info); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     return applist; 
    } 

    private class LoadApplications extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog progress = null; 

     @Override 
     protected Void doInBackground(Void... params) { 
      applist = checkForLaunchIntent(packageManager 
        .getInstalledApplications(PackageManager.GET_META_DATA)); 
      listadaptor = new ApplicationAdapter(
        getActivity().getBaseContext(), R.layout.snippet_list_row, 
        applist); 

      return null; 
     } 

     @Override 
     protected void onCancelled() { 
      super.onCancelled(); 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

      sh = getActivity().getBaseContext().getSharedPreferences(
        MY_PREFERENCES, Context.MODE_PRIVATE); 
      int sortOrder = sh.getInt("sort_type", 0); 
      switch (sortOrder) { 
      case 1: 
       listadaptor.sortLex(); 
       break; 
      case 2: 
       listadaptor.sortLexDesc(); 
       break; 

      case 3: 
       listadaptor.sortRating(); 
       break; 

      case 4: 
       listadaptor.sortRaingDesc(); 
       break; 

      default: 
      } 

      setListAdapter(listadaptor); 
      progress.dismiss(); 
      super.onPostExecute(result); 
     } 

     @Override 
     protected void onPreExecute() { 
      progress = ProgressDialog.show(getActivity().getBaseContext(), 
        null, "Ładujemy!!"); 
      super.onPreExecute(); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 
    } 
} 

android.app.Activity에서

캐스트 할 수없는

public class HostActivity extends Activity implements OnItemSelectedListener{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_fragment); 
    } 

    @Override 
    public void onRssItemSelected(String link) { 
     // TODO Auto-generated method stub 

    } 
} 

main_fragment.xml

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

    <fragment 
     android:id="@+id/country_fragment" 
     android:name="com.android.homework.AllAppsFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 
+0

'AndroidManifest.xml'을 (를) 볼 수 있습니까? –

답변

1

예외는 응용 프로그램 시작에 발생합니다. 그것은 주 활동을 시작하려고 시도하지만, 어떤 이유로, 조각을 시작하려고합니다. Activity으로 캐스팅하지만 FragmentActivity을 확장하지 않습니다. 당신이 한 것은 조각을 액티비티로 AndroidManifest.xml에 추가 한 것입니다. 이것을 HostActivity으로 변경하십시오.

관련 문제