簡體   English   中英

切換到活動時出現黑屏

[英]Getting black screen when switching to an activity

如標題所示,嘗試切換到新活動時,我得到了黑屏。 奇怪的是,我能夠毫無問題地切換到另一個(幾乎相同的活動)。 (它正在模擬器上運行。)

我最好的假設是新活動的onCreate()方法中有某些內容(但其他活動的onCreate方法或多或少都相同,並且可以正常工作),或者由於某種原因,它試圖一次又一次地運行新活動而未成功內存不足,但我不知道是什么原因。

這是切換到新活動的代碼,第一個嵌套的if語句毫無問題地轉到新活動,第二個則顯示黑屏:

menu.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            //check which menu item was pressed

            String group = (String)((PBMMenuItem)parent.getExpandableListAdapter().getGroup(groupPosition)).name;
            String child = (String)parent.getExpandableListAdapter().getChild(groupPosition, childPosition);

            String addSession = getString(R.string.addsession);

            //set up intent
            Intent intent;

            if(group.equals(getString(R.string.cashgames))) {
                if(child.equals(getString(R.string.addsession)))
                {
                    intent = new Intent(v.getContext(), AddCashGame.class);
                    startActivity(intent);
                }
                else if (child.equals(getString(R.string.viewsessions)))
                {
                    intent = new Intent(v.getContext(), ViewSessions.class);
                    startActivity(intent);
                }
            }
          //else if(group.equals(getString(R.string.tournaments)))
          //{
          //
          //}

            return true;
        }
    });

這是無法正常啟動的活動的類代碼:

public class ViewSessions extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**Fragment managing the behaviors, interactions and presentation of the navigation drawer.*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**Used to store the last screen title. For use in {@link #restoreActionBar()}.*/
private CharSequence mTitle;

private final Context C = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_sessions);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}

public void openNavDrawer(View view)
{
    mNavigationDrawerFragment.getDrawerLayout().openDrawer(Gravity.LEFT);
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {

}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.view_sessions, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_sessions, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((ViewSessions) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}

這是活動的XML:

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

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

    <RelativeLayout
        style="@style/pokertable_main"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical|right">

    </RelativeLayout>

    <LinearLayout
        style="@style/pokertable_menu"
        android:orientation="horizontal"
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|center_vertical"
        android:weightSum="1">

        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageButton"
            android:layout_gravity="center_vertical"
            android:src="@drawable/right_facing_arrow_icon"
            android:onClick="openNavDrawer"
            android:background="@android:color/transparent" />

    </LinearLayout>

</FrameLayout>

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="pokerbankrollmanager.com.pokerbankrollmanager.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

當我單擊以切換到新活動並出現黑屏時,我的Logcat會瘋狂地打印如下消息:

07/15/21:07:27.092 1511-1522 / pbm.com.pbm I / art:背景部分並發標記清除GC已釋放14484(873KB)AllocSpace對象,61(1464KB)LOS對象,5%的可用空間,33MB / 35MB,暫停1.617ms共109.674ms

07-15 21:07:27.614 1511-1522 / pbm.com.pbm W / art:掛起所有線程:7.923毫秒

07-15 21:07:27.644 1511-1522 / pbm.com.pbm I / art:背景部分並發標記清除GC已釋放8838(541KB)AllocSpace對象,41(984KB)LOS對象,5%已釋放,34MB / 36MB,暫停11.304ms共76.280ms

07-15 21:07:31.270 1511-1518 / pbm.com.pbm W / art:暫停所有線程所花時間:9.136ms

07-15 21:07:31.782 1511-1518 / pbm.com.pbm W / art:暫停所有線程花費:20.296ms

您好yitzih,我認為您的XML文件中的錯誤行在此行中給出了錯誤的上下文

tools:context="pokerbankrollmanager.com.pokerbankrollmanager.AddCashGame">

這應該是

tools:context="pokerbankrollmanager.com.pokerbankrollmanager.ViewSessions">`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM