所以,我用这段代码练习,我从来不知道哪里出错了。我遵循了本教程中的确切内容
这是我的代码:应用程序意外停止。请帮忙
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private MenuItem settings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
private boolean isResumed = false;
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SPLASH, false);
}
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(settings)) {
showFragment(SETTINGS, true);
return true;
}
return false;
}
}
05-21 05:46:22.031: D/dalvikvm(527): GC_EXTERNAL_ALLOC freed 89K, 52% free 2603K/5379K, external 1808K/2137K, paused 89ms
05-21 05:46:22.221: D/AndroidRuntime(527): Shutting down VM
05-21 05:46:22.221: W/dalvikvm(527): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-21 05:46:22.241: E/AndroidRuntime(527): FATAL EXCEPTION: main
05-21 05:46:22.241: E/AndroidRuntime(527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firstandroidapp/com.firstandroidapp.MainActivity}: java.lang.NullPointerException
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.os.Looper.loop(Looper.java:123)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 05:46:22.241: E/AndroidRuntime(527): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 05:46:22.241: E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 05:46:22.241: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 05:46:22.241: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 05:46:22.241: E/AndroidRuntime(527): at dalvik.system.NativeStart.main(Native Method)
05-21 05:46:22.241: E/AndroidRuntime(527): Caused by: java.lang.NullPointerException
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:609)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.Activity.performStart(Activity.java:3791)
05-21 05:46:22.241: E/AndroidRuntime(527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
05-21 05:46:22.241: E/AndroidRuntime(527): ... 11 more
05-21 05:47:01.391: I/Process(527): Sending signal. PID: 527 SIG: 9
我也有同样的问题。
我解决问题的方法是首先进行调试,看看我的任何片段,“飞溅”或“选择”是否为空。
并且它们是空的。
原因是,在我的Fragments类中,当我应该导入“android.support.v4.app.Fragment”时,我导入了“android.app.Fragment”
堆栈溢出的两个答案都帮助我解决了我的问题:
Android fragment manager backstackrecord . run抛出NullPointerException
findFragmentById返回null
Composite Images watermark(['/img/shepherd.jpg', '/img/logo.png']) .image(watermark.image.lowerRight()) .then(function (img) { document.getElementById('composite-image').appendChild(img); }); Al
示例的Python源代码或者交互界面都可以使用标准reST模块实现.在正常段落后面跟着 :: 开始,再加上适当缩进. 交互界面需包含提示及Python代码的输出. 交互界面没有特别的标记. 在最后一行输入或输出之后,不应出现空的提示; 这是一个什么都不做的例子: >>> 1 + 1 2 >>> 语法高亮显示由 Pygments (如果安装) 优雅的显示: 每个源文件都有高亮语言”highlight
Redux 源码 中同时包含了一些示例。这些示例中的大多数也在CodeSandbox上,这是一个在线编辑器,可让您在线测试示例。 原生版 Counter 运行 Counter Vanilla 示例: git clone https://github.com/reactjs/redux.git cd redux/examples/counter-vanilla open index.html 该示
这是一些 Mithril 的示例: Animation DBMonster Markdown Editor SVG: Clock, Ring, Tiger ThreadItJS TodoMVC
已经有超过50,000本图书使用GitBook.com发布。 文档 DuckDuckHack 文档 by DuckDuckGo Loomio Handbook and guide to using Loomio both by Loomio Enspiral Handbook by Enspiral Webmagic开发文档 by https://github.com/code4craft/web
这里列出了所有示例: adc_vol_sample.c dynmem_sample.c event_sample.c httpclient_sample.c hwtimer_sample.c i2c_aht10_sample.c idlehook_sample.c interrupt_sample.c iwdg_sample.c led_blink_sample.c mailbox_sample.
下面的例子说明了部署描述文件模式中列出的定义的用法。 一个简单的例子 CODE EXAMPLE 14-1 Basic Deployment Descriptor Example <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://
计算变量 首先,让我们从一些必要的代码开始。 这个例子的目的是如果条件满足,将 a 和 b 计算后的值绑定到 c 上。 下面就是必要的代码示例: // this is standard imperative code var c: String var a = 1 // this will only assign the value `1` to `a` once var b = 2