gpt4 book ai didi

java - 切换到 Activity 时出现黑屏

转载 作者:行者123 更新时间:2023-12-02 04:31:11 25 4
gpt4 key购买 nike

正如标题所述,我在尝试切换到新 Activity 时遇到黑屏。奇怪的是我能够毫无问题地切换到不同的(几乎相同的 Activity )。 (它正在模拟器上运行。)

我最好的假设是新 Activity 的 onCreate() 方法中的某些内容(但另一个 Activity 的 onCreate 方法或多或少相同并且工作正常)或由于某种原因,它无法成功地尝试一遍又一遍地启动新 Activity ,并且内存不足,但我不知道是什么或为什么。

这是切换到新 Activity 的代码,第一个嵌套 if 语句没有问题地转到新 Activity ,第二个则出现黑屏:

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;
}
});

这是无法正常启动的 Activity 的类代码:

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));
}
}

}

这是该 Activity 的 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" />

当我单击切换到新 Activity 并黑屏时,我的 Logcat 会疯狂地打印如下消息:

07-15 21:07:27.092 1511-1522/pbm.com.pbm I/art﹕ Background partial concurrent mark sweep GC freed 14484(873KB) AllocSpace objects, 61(1464KB) LOS objects, 5% free, 33MB/35MB, paused 1.617ms total 109.674ms

07-15 21:07:27.614 1511-1522/pbm.com.pbm W/art﹕ Suspending all threads took: 7.923ms

07-15 21:07:27.644 1511-1522/pbm.com.pbm I/art﹕ Background partial concurrent mark sweep GC freed 8838(541KB) AllocSpace objects, 41(984KB) LOS objects, 5% free, 34MB/36MB, paused 11.304ms total 76.280ms

07-15 21:07:31.270 1511-1518/pbm.com.pbm W/art﹕ Suspending all threads took: 9.136ms

07-15 21:07:31.782 1511-1518/pbm.com.pbm W/art﹕ Suspending all threads took: 20.296ms

最佳答案

您好 yitzih,我认为您的 XML 文件中有错误的行,您在该行中给出了错误的上下文

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

this should be

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

关于java - 切换到 Activity 时出现黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31444016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com