gpt4 book ai didi

Android 应用程序在方向更改时重新启动

转载 作者:行者123 更新时间:2023-11-29 15:18:19 27 4
gpt4 key购买 nike

<分区>

我的应用程序包含 fragment ,它主要是 fragment 应用程序。我只有3个 Activity ,其他都是 fragment 。但是,当我在横向模式或纵向模式下旋转手机时,从应用程序的任何一点开始,应用程序都会重新启动并从应用程序的起点继续,从头开始。我把 android:configChanges="orientation|keyboardHidden|keyboard" 放在 Manifest 中,但它似乎根本没有效果。我只用一个 WebView 填充 fragment 。但我也改变了 Action 条的颜色。这就是我在应用程序中所做的一切。这是一个静态应用程序,没有用户输入。这是我的 fragment 的示例代码:

public class CLASSNAME extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.LAYOUT, container, false);
ActionBar bar = getActivity().getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.COLOR)));
bar.setTitle("TITLE");


final WebView wFirst = (WebView) view.findViewById(R.id.WEBVIEWID);
wFirst.getSettings().setJavaScriptEnabled(true);
wFirst.setBackgroundColor(0x00000000);

wFirst.loadDataWithBaseURL(LOADING DATA);
wFirst.setWebViewClient(new WebViewClient() {SETTING UP WEBVIEW CLIENT});

return view;
}
}

我阅读了很多通过保存实例状态和其他方法得到解答的帖子,但就我而言,我不知道要保存什么,也不知道如何在方向改变的情况下保持我的 Activity 滚动。

这是 MainActivity.class 代码:

public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;

private static CharSequence mTitle;
public static Context context;

public static Context getContext() {
return context;
}

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

context = getApplicationContext();

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

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

@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}

@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();

switch (position) {
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, new AlgebraicGraphs())
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, new Logarithms())
.commit();
break;
case 3:
fragmentManager.beginTransaction()
.replace(R.id.container, new Polynomials())
.commit();
break;
case 4:
fragmentManager.beginTransaction()
.replace(R.id.container, new Powers())
.commit();
break;
case 6:
fragmentManager.beginTransaction()
.replace(R.id.container, new AreaFormulas())
.commit();
break;
case 7:
fragmentManager.beginTransaction()
.replace(R.id.container, new SurfaceAreaFormulas())
.commit();
break;
case 8:
fragmentManager.beginTransaction()
.replace(R.id.container, new PerimeterFormulas())
.commit();
break;
case 9:
fragmentManager.beginTransaction()
.replace(R.id.container, new VolumeFormulas())
.commit();
break;
case 11:
fragmentManager.beginTransaction()
.replace(R.id.container, new TrigonometryGraphsFormulas())
.commit();
break;
case 12:
fragmentManager.beginTransaction()
.replace(R.id.container, new HyperbolicIdentities())
.commit();
break;
case 13:
fragmentManager.beginTransaction()
.replace(R.id.container, new TrigonometricIdentities())
.commit();
break;
case 16:
fragmentManager.beginTransaction()
.replace(R.id.container, new IntegralIdentities())
.commit();
break;
case 17:
fragmentManager.beginTransaction()
.replace(R.id.container, new IntegralSpecialFunctions())
.commit();
break;
case 18:
fragmentManager.beginTransaction()
.replace(R.id.container, new TableOfIntegrals())
.commit();
break;
case 20:
fragmentManager.beginTransaction()
.replace(R.id.container, new DerivativeIdentities())
.commit();
break;
case 21:
fragmentManager.beginTransaction()
.replace(R.id.container, new TableOfDerivatives())
.commit();
break;
default:
fragmentManager.beginTransaction()
.replace(R.id.container, new DefaultLayout())
.commit();
break;
}
}

@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.main, menu);
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();
if (id == R.id.action_about) {
About about = new About();
about.show(getFragmentManager(), "about");
}
return false;
}

/**
* 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;
}

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

这是日志:

02-19 06:41:24.849    9788-9788/l.pocketformulas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment Logarithms{4132f5d0} not attached to Activity
at android.app.Fragment.getResources(Fragment.java:828)
at l.pocketformulas.Logarithms$1.onPageFinished(Logarithms.java:71)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:444)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)

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