0

我对Java很缺乏经验,接近完成我的第一个Android应用程序,当我最怕的事情发生时,我遇到了一个我不知道如何解释的错误。类别演员异常与片段列表和活动

该应用程序的基础是从抽屉菜单中选择的5个列表。每个列表都打开静态XML视图,其中包含内容。所有这些都能顺利地在肖像中运作在风景中,视图是一个双面板,左边是列表,右边是内容。到那里工作也很好。但是,当我在一个列表中进入横向时出现错误,然后在另一个列表中尝试进入横向模式。有人可以试着帮我弄清楚发生了什么吗?

主要活动

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.res.Configuration; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.actionbarsherlock.view.Menu; 
import com.actionbarsherlock.view.MenuItem; 

public class MainActivity extends SherlockFragmentActivity { 

    Fragment rotcfraglist = new RotcFragList(); 
    Fragment afafraglist = new AfaFragList(); 
    Fragment bmtfraglist = new BmtFragList(); 
    Fragment docsfraglist = new DocsFragList(); 
    Fragment extrafraglist = new ExtraFragList(); 

.... *drawer set up stuff .... 

// ListView click listener in the navigation drawer 
    private class DrawerItemClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      selectItem(position); 
     } 
    } 

    private void selectItem(int position) { 

     FragmentManager fm = getSupportFragmentManager(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 

     View fullFrame = findViewById(R.id.full_frame); 
     View detailsFrame = findViewById(R.id.details); 
     View listFrame = findViewById(R.id.list_frame); 

     // Destroy Backstack first 
     fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

     // Locate Position 
     switch (position) { 
     ///ROTC 
     case 0: 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrame != null 
        && detailsFrame.getVisibility() == View.GONE) { 
       detailsFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

     ft.replace(R.id.list_frame, rotcfraglist); 
      break; 
     ///AFA 
     case 1: 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrame != null 
        && detailsFrame.getVisibility() == View.GONE) { 
       detailsFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

      ft.replace(R.id.list_frame, afafraglist); 
      break; 
     ///BMT 
     case 2: 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrame != null 
        && detailsFrame.getVisibility() == View.GONE) { 
       detailsFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

      ft.replace(R.id.list_frame, bmtfraglist); 
      break; 
     ///Docs 
     case 3: 
      ft.remove(rotcfraglist); 
      ft.remove(afafraglist); 
      ft.remove(bmtfraglist); 

      if (detailsFrame != null 
        && detailsFrame.getVisibility() == View.VISIBLE) { 
       detailsFrame.setVisibility(View.GONE); 
      } 
      if (fullFrame != null && fullFrame.getVisibility() == View.GONE) { 
       fullFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) { 
       listFrame.setVisibility(View.GONE); 
      } 

      ft.replace(R.id.full_frame, docsfraglist); 
      break; 
     ///Extra 
     case 4: 
      ft.remove(rotcfraglist); 
      ft.remove(afafraglist); 
      ft.remove(bmtfraglist); 

      if (detailsFrame != null 
        && detailsFrame.getVisibility() == View.VISIBLE) { 
       detailsFrame.setVisibility(View.GONE); 
      } 
      if (fullFrame != null && fullFrame.getVisibility() == View.GONE) { 
       fullFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) { 
       listFrame.setVisibility(View.GONE); 
      } 

      ft.replace(R.id.full_frame, extrafraglist); 
      break; 
     } 
     ft.commit(); 
     mDrawerList.setItemChecked(position, true); 

     // Get the title followed by the position 
     setTitle(title[position]); 
     // Close drawer 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

一个列表片段的

import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.app.SherlockFragmentActivity; 
import com.actionbarsherlock.app.SherlockListFragment; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v4.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class BmtFragList extends SherlockListFragment { 

boolean mDualPane; 
static int mCurCheckPosition = 0; 
public final String [] TITLES = { 
    "Core Values", 
    "AF Mission", 
    "Airman's Creed", 
    "Air Force Song", 
    "Ranks AF Enlisted", 
    "Ranks AF Officer", 
    "Chain of Command", 
    "Reporting Statement", 
    "Phonetic Alphabet" 
}; 

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

    setListAdapter(new ArrayAdapter<String>(getActivity(), 
      R.layout.simple_list_item_checkable_1, android.R.id.text1, TITLES)); 

    // Check landscape or not 
    View detailsFrame = getActivity().findViewById(R.id.details); 
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE; 

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    if (savedInstanceState != null) { 
     mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); 
     showDetails(mCurCheckPosition); 
    } 

    if (mDualPane) { 
     showDetails(mCurCheckPosition); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    outState.putInt("curChoice", mCurCheckPosition); 
} 

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

// If landscape, replace details. If not, replace list. 
void showDetails(int position) { 

    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
    mCurCheckPosition = position; 

    if (mDualPane) { 
     getListView().setItemChecked(position, true); 

     BmtDetailsFragment details = (BmtDetailsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.details); 

     if (details == null || details.getShownIndex() != position) { 
      details = BmtDetailsFragment.newInstance(position); 
      ft.replace(R.id.details, details); 
      ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
      ft.commit(); 
     } 
    } else { 
     getListView().setItemChecked(position, true); 

     Intent intent = new Intent(); 
     intent.setClass(getActivity(), DetailsActivity.class); 
     intent.putExtra("position", position); 
     startActivity(intent); 
    } 
} 

public static class DetailsActivity extends SherlockFragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     setTheme(MainActivity.getThemeId()); 

     super.onCreate(savedInstanceState); 

     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      finish(); 
      return; 
     } 
     if (savedInstanceState == null) { 
      BmtDetailsFragment details = new BmtDetailsFragment(); 
      details.setArguments(getIntent().getExtras()); 
      getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit(); 
     } 
    } 
} 

public static class BmtDetailsFragment extends SherlockFragment { 

    public static BmtDetailsFragment newInstance(int position) { 
     BmtDetailsFragment f = new BmtDetailsFragment(); 

     // Make index input an argument 
     Bundle args = new Bundle(); 
     args.putInt("position", position); 
     f.setArguments(args); 

     return f; 
    } 

    public int getShownIndex() { 
     return getArguments().getInt("position", 0); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     if (container == null) { 
     return null; 
     } 

     View newContent = getView(); 
     int position = getShownIndex(); 

     switch (position) { 
     case 0: 
      newContent = inflater.inflate(R.layout.corevalues, container, false); 
      break; 
     case 1: 
      newContent = inflater.inflate(R.layout.afmission, container, false); 
      break; 
     case 2: 
      newContent = inflater.inflate(R.layout.airmanscreed, container, false); 
      break; 

    ....more cases..... 

     } 
     return newContent; 
    } 
} 
} 

主要活动景观布局

<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<FrameLayout 
    android:id="@+id/full_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/list_frame" 
     android:layout_width="180dp" 
     android:layout_height="match_parent" /> 

    <FrameLayout 
     android:id="@+id/details" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/list_frame" > 
    </FrameLayout> 
</RelativeLayout> 

<ListView 
    android:id="@+id/listview_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#262626" 
    android:choiceMode="singleChoice" 
    android:divider="#5C5C5C" 
    android:dividerHeight="1dp" /> 

</android.support.v4.widget.DrawerLayout> 

logcat的

enter image description here

对不起,我的代码在墙上,但我真的失去了这里。我的猜测是这与列表旁边的细节框有关,因为错误指向每个类中的DetailsActivity,负责设置该框架中的内容。也许我需要清除框架,然后才能将新的详细信息加载到同一个地方?但我不确定如何去解决这个问题。任何帮助将不胜感激!我不想看到我的第一个应用程序像这样崩溃,我觉得生病考虑。

回答

0

找出解决方案!也许它不是最漂亮的,但它的工作原理,这比崩溃更好。

我确定每个listfragment我都不喜欢“共享”横向内容框架,所以我让每个列表都是自己的框架布局来放入它的片段。当一个新的列表被调用时,其他框架布局的可见性被设置为“GONE”并且基本上被置于后台。稍后回电时,他们会显示他们之前持有的内容,这是一个很好的奖励。

这里是在MainActivity

// ListView click listener in the navigation drawer 
    private class DrawerItemClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      selectItem(position); 
     } 
    } 

    private void selectItem(int position) { 

     FragmentManager fm = getSupportFragmentManager(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 

     View fullFrame = findViewById(R.id.full_frame); 
     View detailsFrameRotc = findViewById(R.id.details_rotc); 
     View listFrame = findViewById(R.id.list_frame); 
     View detailsFrameAfa = findViewById(R.id.details_afa); 
     View detailsFrameBmt = findViewById(R.id.details_bmt); 

     // Destroy Backstack first 
     fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

     // Locate Position 
     switch (position) { 
     ///ROTC 
     case 0: 
      ft.replace(R.id.list_frame, rotcfraglist); 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrameAfa != null 
        && detailsFrameAfa.getVisibility() == View.VISIBLE) { 
       detailsFrameAfa.setVisibility(View.GONE); 
      } 
      if (detailsFrameBmt != null 
        && detailsFrameBmt.getVisibility() == View.VISIBLE) { 
       detailsFrameBmt.setVisibility(View.GONE); 
      } 
      if (detailsFrameRotc != null 
        && detailsFrameRotc.getVisibility() == View.GONE) { 
       detailsFrameRotc.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

      break; 
     ///AFA 
     case 1: 
      ft.replace(R.id.list_frame, afafraglist); 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrameRotc != null 
        && detailsFrameRotc.getVisibility() == View.VISIBLE) { 
       detailsFrameRotc.setVisibility(View.GONE); 
      } 
      if (detailsFrameBmt != null 
        && detailsFrameBmt.getVisibility() == View.VISIBLE) { 
       detailsFrameBmt.setVisibility(View.GONE); 
      } 
      if (detailsFrameAfa != null 
        && detailsFrameAfa.getVisibility() == View.GONE) { 
       detailsFrameAfa.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

      break; 
     ///BMT 
     case 2: 
      ft.replace(R.id.list_frame, bmtfraglist); 
      ft.remove(docsfraglist); 
      ft.remove(extrafraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) { 
       fullFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrameAfa != null 
        && detailsFrameAfa.getVisibility() == View.VISIBLE) { 
       detailsFrameAfa.setVisibility(View.GONE); 
      } 
      if (detailsFrameRotc != null 
        && detailsFrameRotc.getVisibility() == View.VISIBLE) { 
       detailsFrameRotc.setVisibility(View.GONE); 
      } 
      if (detailsFrameBmt != null 
        && detailsFrameBmt.getVisibility() == View.GONE) { 
       detailsFrameBmt.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.GONE) { 
       listFrame.setVisibility(View.VISIBLE); 
      } 

      break; 
     ///Docs 
     case 3: 
      ft.replace(R.id.full_frame, docsfraglist); 
      ft.remove(rotcfraglist); 
      ft.remove(afafraglist); 
      ft.remove(bmtfraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.GONE) { 
       fullFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) { 
       listFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrameAfa != null 
        && detailsFrameAfa.getVisibility() == View.VISIBLE) { 
       detailsFrameAfa.setVisibility(View.GONE); 
      } 
      if (detailsFrameBmt != null 
        && detailsFrameBmt.getVisibility() == View.VISIBLE) { 
       detailsFrameBmt.setVisibility(View.GONE); 
      } 
      if (detailsFrameRotc != null 
        && detailsFrameRotc.getVisibility() == View.VISIBLE) { 
       detailsFrameRotc.setVisibility(View.GONE); 
      } 

      break; 
     ///Extra 
     case 4: 
      ft.replace(R.id.full_frame, extrafraglist); 
      ft.remove(rotcfraglist); 
      ft.remove(afafraglist); 
      ft.remove(bmtfraglist); 

      if (fullFrame != null && fullFrame.getVisibility() == View.GONE) { 
       fullFrame.setVisibility(View.VISIBLE); 
      } 
      if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) { 
       listFrame.setVisibility(View.GONE); 
      } 
      if (detailsFrameAfa != null 
        && detailsFrameAfa.getVisibility() == View.VISIBLE) { 
       detailsFrameAfa.setVisibility(View.GONE); 
      } 
      if (detailsFrameBmt != null 
        && detailsFrameBmt.getVisibility() == View.VISIBLE) { 
       detailsFrameBmt.setVisibility(View.GONE); 
      } 
      if (detailsFrameRotc != null 
        && detailsFrameRotc.getVisibility() == View.VISIBLE) { 
       detailsFrameRotc.setVisibility(View.GONE); 
      } 

      break; 
     } 
     ft.commit(); 
     mDrawerList.setItemChecked(position, true); 

     // Get the title followed by the position 
     setTitle(title[position]); 
     // Close drawer 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } 

的变化,我的景观布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<FrameLayout 
    android:id="@+id/full_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/list_frame" 
     android:layout_width="180dp" 
     android:layout_height="match_parent" /> 

    <FrameLayout 
     android:id="@+id/details_rotc" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/list_frame" > 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/details_afa" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/list_frame" > 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/details_bmt" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/list_frame" > 
    </FrameLayout> 
</RelativeLayout> 

<ListView 
    android:id="@+id/listview_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#262626" 
    android:choiceMode="singleChoice" 
    android:divider="#5C5C5C" 
    android:dividerHeight="1dp" /> 

</android.support.v4.widget.DrawerLayout>