2012-02-03 73 views
0

我有一个片段视图,需要用另一个片段替换。片段错误 - ListFragment不能转换为android.app.Activity

当选择ListFragment项目时,DetailsFragment将被替换为另一个ListFragment,方法是将其他事件传递给活动中新的(或第二个)ListFragment。我的问题是,我得到了“未发现处理意图{(有额外)}”的活动。当活动首次启动时,ListFragment可以正常工作,但是当我使用另一个ListFragment进行日期(替换)Details活动时,出现错误。

这是我的第一个片段活动,我猜我不知道如何正确传递片段之间的其他东西。我肯定没有正确使用fragment-manager/transaction类(?)。如果有人能纠正我的执行情况,我将不胜感激。

更新:我添加了“i.setClass(getActivity(),ListFragment.class);”在ListFragment类的意图,现在日志错误已更改为以下几点:

UPDTATE 2:我纠正了我的意图,以参数作为Devunwired sugested,现在工作得漂亮。 Thnx Devunwired。我现在唯一的问题是,当后退键被按下时,后台堆栈不起作用。校正类如下:

logcat的(修订版)

 FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.andaero.test/com.andaero.test.fragments.ListFragment}: java.lang.ClassCastException: com.andaero.test.fragments.ListFragment cannot be cast to android.app.Activity 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 
    at android.app.ActivityThread.access$500(ActivityThread.java:122) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:132) 
    at android.app.ActivityThread.main(ActivityThread.java:4123) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:491) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.ClassCastException: com.andaero.test.fragments.ListFragment cannot be cast to android.app.Activity 

的ListFragment类:

public class ListFragment extends android.app.ListFragment { 

    boolean mDualPane; 
    int mCurCheckPosition = 0; 
    protected TextView activityTitle; 

    boolean mExternalStorageAvailable = false; 
    boolean mExternalStorageWriteable = false; 

    String extStorageDirectory = Environment.getExternalStorageDirectory() 
      .toString(); 
    File dbfile = new File(extStorageDirectory + "/Andaero/dB/Andaero.db"); 
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 

    private static final String QUERY_KEY = "QUERY_KEY"; 
    private static final String QUERY_ORDER = "QUERY_ORDER"; 

    private View layout; 

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

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     Bundle extras = getActivity().getIntent().getExtras(); 
     Bundle arg = this.getArguments();//**ADDED TO GET THE ARGS 

     /** 
     * Get the query string from last activity and pass it to this 
     * activity----------------------------------------------------- 
     */ 
     String q = null; 
     if (extras != null) { 
      q = extras.getString(QUERY_KEY); 
     } 

     if (arg != null) { 
     q = (String) (getArguments() != null ? getArguments().getString(
       "QUERY_KEY") : 1); 
    } 

     loadQuery(q); 
    } 

    public void loadQuery(String q) { 

     if (Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) { 

      String qO = getActivity().getIntent().getStringExtra("QUERY_ORDER"); 
      Cursor c = db.rawQuery(q + " ORDER BY `_id` " + qO, null); 
      setListAdapter(new QueryAdapter(getActivity(), c)); 
      db.close(); 

     } else { 
      Alerts.sdCardMissing(getActivity()); 
     } 
    } 

    public class QueryAdapter extends CursorAdapter { 

     public QueryAdapter(Context context, Cursor c) { 
      super(context, c); 
      LayoutInflater.from(context); 
     } 

     @Override 
     public void bindView(View v, Context context, final Cursor c) { 

      int tvLabel = c.getColumnIndexOrThrow("label"); 
      String label = c.getString(tvLabel); 
      final TextView labelTxt = (TextView) v.findViewById(R.id.label); 

      if (labelTxt != null) { 
       labelTxt.setText("(" + label + ")"); 
      } 

      int tvTitle = c.getColumnIndexOrThrow("title"); 
      final String title = c.getString(tvTitle); 
      TextView titleTxt = (TextView) v.findViewById(R.id.listTitle); 

      if (titleTxt != null) { 
       titleTxt.setText(title); 
      } 

      int tvDescription = c.getColumnIndexOrThrow("description"); 
      String description = c.getString(tvDescription); 
      TextView descriptionTxt = (TextView) v.findViewById(R.id.caption); 

      if (descriptionTxt != null) { 
       descriptionTxt.setText(description); 
      } 

      int tvDate = c.getColumnIndexOrThrow("date"); 
      String date = c.getString(tvDate); 
      TextView dateTxt = (TextView) v.findViewById(R.id.dateAdded); 

      if (dateTxt != null) { 
       dateTxt.setText(date); 
      } 

      int tvGoto = c.getColumnIndexOrThrow("gotoURL"); 
      final String gotoURL = c.getString(tvGoto); 
      TextView gotoTxt = (TextView) v.findViewById(R.id.dummy); 

      if (gotoTxt != null) { 
       gotoTxt.setText(gotoURL); 
      } 

      gotoTxt.setVisibility(View.GONE); 
      v.setTag(gotoURL); 

      final ListView lv = getListView(); 
      lv.setEnabled(true); 
      lv.setClickable(true); 

      lv.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> arg0, View v, int arg2, 
         long arg3) { 

        // Create new fragment and transaction 
        Fragment newFragment = new ListFragment(); 
        FragmentTransaction transaction = getFragmentManager() 
          .beginTransaction(); 

        // Replace whatever is in the fragment_container view with 
        // this fragment, 
        // and add the transaction to the back stack 
        transaction.replace(R.id.detailFragment, newFragment); 
        transaction.addToBackStack(null); 

        String url = ""; 
        url = (String) v.getTag(); 

        int nI = c.getColumnIndexOrThrow("intent"); 
        String intent = c.getString(nI); 
        Class<?> myIntent = null; 
        try { 
         myIntent = Class.forName("com.andaero.test.fragments" 
           + intent); 
        } catch (ClassNotFoundException e) { 
         Log.e("ERROR", "Class Not Found for new intent!"); 
         e.printStackTrace(); 
        } 

        int tvTitle = c.getColumnIndexOrThrow("title"); 
        String title = c.getString(tvTitle); 

        int tvLabel = c.getColumnIndexOrThrow("label"); 
        String label = c.getString(tvLabel); 

        String queryKey = "SELECT * FROM " + label; 
        c.close(); 
        db.close(); 

        Bundle args = new Bundle();//**REPLACED THE INTENTS 
        args.putString("QUERY_KEY", queryKey); 
        args.putString("KEY_URL", url); 
        args.putString("KEY_SUBTITLE", title); 
        args.putString("KEY_LABEL", label); 
        args.putString("KEY_INTENT", intent); 
        args.putString("QUERY_ORDER", "ASC"); 

        newFragment.setArguments(args); 

        // Commit the transaction 
        transaction.commit(); 
       } 
      }); 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      final View v = LayoutInflater.from(context).inflate(
        R.layout.list_item, parent, false); 
      return v; 
     } 
    } 
} 

回答

0

你是在正确的轨道上使用FragmentTransaction更换一次Fragment与其他的东西,但Fragment不使用Intent从一个实例的数据传递到另一个像Activity确实。事实上,拨打startActivity()Intent指向Fragment将导致你不想要的各种烟花(如你所见过的)。

在构造函数中将数据传递给新的Fragment是完全可以的,因此您可以为您的ListFragment创建一个构造函数,该构造函数接受要转发的任何数据参数。另一种选择是将所有“额外”设置为新的Fragment上的参数,方法是将它们放入Bundle并使用Fragment.setArguments()。只要您想访问您附加到Fragment的参数,您只需拨打即可获取相同的Bundle。此外

Bundle args = new Bundle(); 
args.putString("QUERY_KEY", queryKey); 
//...add all the extras to the bundle 

newFragment.setArguments(args); 

transaction.commit(); 

,偏离主题,但你可能想使你不给你Fragment重命名为别的东西:所以基本上,替换所有具有带有Intent做的代码在你的onItemClick()方法,而是不得不依赖完全限定的软件包名称来区分ListFragment和代码中的平台版本。

HTH

+0

Thnx在这方面有帮助。我现在没有得到错误,并且活动加载了,但是它所做的只是重新加载当前存在的数据:它不会将新数据加载到列表中,就像当类设置了w/o片段时一样。任何更多的帮助/指针。 Thnx – CelticParser 2012-02-06 21:38:35

+0

再次thnx帮助与此。你可以看看我的改变,看看是否有更好的方法来实现我的参数,并且你是否明白为什么我的后退堆栈不能使用后退键? – CelticParser 2012-02-07 00:17:08

+0

我没有看到任何明显的后退动作不起作用。该事务正被添加到后退堆栈,因此点击后退按钮应该将最后一个事务弹出到其先前的状态。你是否在你的Activity中覆盖了'onBackPressed()'而不调用'super'或其他会影响标准回退行为的东西? – Devunwired 2012-02-07 15:05:17

0

你为什么不只是创建您传递的价值观进入,当你一个构造函数创建片段?这似乎是你正在寻找做

+0

因为它不好使用片段中的构造函数。你总是需要默认的构造函数(空),或者它会在恢复时破坏... – Warpzit 2012-06-15 10:22:22

相关问题