Listview内部片段崩溃

时间:2014-01-14 19:23:51

标签: android android-fragments fragment

我刚刚开始使用android,并尝试在其片段中创建带有listview的选项卡式布局。什么是最好的选择。? 我试过下面哪个有效: 1.为listview制作自定义适配器 2.带有虚拟片段的双标签动作栏布局 当我尝试将listview活动用作片段时,应用程序崩溃了。我已粘贴下面的代码。 应用程序概念:票务预订应用程序!为此,我创建了一个Movie类,其中包含电影名称,类别和评论详细信息。 MovieManager制作电影列表。应用程序在打开片段活动时崩溃,该片段活动将移动列表保存在不同的类别[标签]中。

在打开时崩溃应用的活动:

//包含不同类别中的电影列表的活动 公共类AllMovies扩展了FragmentActivity {

private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
ListViewHollyLV;
ListViewBollyLV;
ListViewKollyLV;

private void ListCreater()
  {
    HollyLV = (ListView)findViewById(R.id.bollyListView1);
    newMovieListActivity().CurrentLV = (ListView)findViewById(R.id.bollyListView1);
  }


  @Override
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_movies);

        ListCreater();


    // Set up the action bar to show tabs.
finalActionBaractionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // for each of the sections in the app, add a tab to the action bar.
    Tab tab = actionBar.newTab().setText(R.string.hello_world).setTabListener(new TabListener( this, "holly", MovieListActivity.class));     
actionBar.addTab(tab); 
tab = actionBar.newTab().setText(R.string.hello_world).setTabListener(new TabListener( this, "bolly", Fragi2.class));     
actionBar.addTab(tab); 
  }


static class TabListener implements ActionBar.TabListener {     
    private Fragment mFragment;     
    private final Activity mActivity;     
    private final String mTag;     
    private final Class mClass;      

      /** Constructor used each time a new tab is created.       
       * * @paramactivity  The host Activity, used to instantiate the fragment       
       * * @paramtag  The identifier tag for the fragment       
       * * @paramclz  The fragment's Class, used to instantiate the fragment       
       * */     

    publicTabListener(Activity activity, String tag, Class clz) 
      {         mActivity = activity;         
    mTag = tag;         
    mClass = clz;     }      

      /* The following are each of the ActionBar.TabListener callbacks */      

    public void onTabSelected(Tab tab, FragmentTransactionft) {         
          // Check if the fragment is already initialized         
        if (mFragment == null) {            
              // If not, instantiate and add it to the activity             
            mFragment = Fragment.instantiate(mActivity, mClass.getName());             
            ft.add(R.id.fragmentCnt, mFragment, mTag);         
              } 
        else {             
              // If it exists, simply attach it in order to show it            
            ft.attach(mFragment);         
              }    
          }      
    public void onTabUnselected(Tab tab, FragmentTransactionft)
      {        
        if (mFragment != null) {         
              // Detach the fragment, because another one is being attached          
            ft.detach(mFragment);        
              }  
      }
    public void onTabReselected(Tab tab, FragmentTransactionft) {      
          // User selected the already selected tab. Usually do nothing.    
          }         
      }

//This fragment is used for “holly” tab
public static class MovieListActivity extends Fragment {

    ListViewCurrentLV;  


    @Override
    public View onCreateView(LayoutInflaterinflater, ViewGroup container,
            Bundle savedInstanceState){
        // Inflate the menu; this adds items to the action bar if it is present.
    View V = inflater.inflate(R.layout.activity_frag1, container, false);
    ManageMovieList.MakeList(); // Makes separate list of Hollywood and bollywood //movies
    LoadMovieList();

return V;

    }               

    @Override 
    public void onPause() {     
        super.onPause(); 

    }       
    //Loads the Hollywood movie list to the listview using custom adapter
    private void LoadMovieList()
    {
        ManageMovieList.HollywoodMovieAdapter(getActivity(), CurrentLV);
    }       
}

/// Dummy Fragment class (was created while trying tabbed layout) which contains only ///textview. Still this fragment is used for “bolly” tab

public static class Fragi1 extends Fragment {


    @Override
    public View onCreateView(LayoutInflaterinflater, ViewGroup container,
            Bundle savedInstanceState){
        // Inflate the menu; this adds items to the action bar if it is present.
    View V = inflater.inflate(R.layout.activity_frag1, container, false);

return V;

    }
}

}

自定义适配器

public class CustomMovieArrayAdapter extends ArrayAdapter {

private final Context context;
ArrayList<Movie> _Movies ;
static final String REVIEW = "com.example.ticketonline";


publicCustomMovieArrayAdapter(Context context, ArrayList<Movie>movieItems) {
    super(context, R.layout.movie_list_item, movieItems);
    this.context = context;
    _Movies = movieItems;
    Movie currentMovie = new Movie();
    ArrayList<Movie> _Movies ;
}

String Title;
String category;
int Id;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflaterinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) { 
        convertView = inflater.inflate(R.layout.movie_list_item, parent, false);

    TextViewtextName = (TextView) convertView.findViewById(R.id.textViewMovieName);
    TextViewtextCate = (TextView) convertView.findViewById(R.id.textViewMovieCategory);
    ImageViewimageView = (ImageView) convertView.findViewById(R.id.imageViewMoviePoster);
    Button btn = (Button)convertView.findViewById(R.id.buttonSelectMovie);

    textName.setText(_Movies.get(position).GetMovieName(position));
    textCate.setText(_Movies.get(position).GetMovieCategory(position));
    imageView.setImageResource(_Movies.get(position).GetMoviePosterID(position));   

    finalint p = position;
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
                   public void onClick(View v) {
Intent intent = new Intent(context, ReviewAndBook.class);    
intent.putExtra(REVIEW, Movie.GetMovieReview(p));
intent.putExtra(android.content.Intent.EXTRA_TEXT, Movie.GetMovieDirector(p));
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, Movie.GetMovieCast(p));
context.startActivity(intent);
        }

    });

    }

    returnconvertView;
}

}

驻留在MovieManager类中的方法

     public static void HollywoodMovieAdapter(Context cnt,ListViewmovieListUI)
{
    //ListViewmovieListUI = (ListView)findViewById(R.id.movieListView);
    ArrayList<Movie> _hollywoodMovies = ManageMovieList.RecentMovies_Holly;
    movieListUI.setAdapter( new CustomMovieArrayAdapter(cnt, _hollywoodMovies));    
}

1 个答案:

答案 0 :(得分:0)

我在有限的经验中看到的一个常见问题是,难以检测的错误可能源自片段布局xml文件。例如,如果存在任何“循环引用”(易于使用RelativeLayout),您的应用程序可能会运行,但是当您打开使用该xml的片段时会崩溃。在Logcat中没有真正指定这种崩溃,它会指向片段中的一行实际上没有问题的代码。代码行创建logcat错误的原因可能是布局xml文件中的错误。