- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个相当简单的程序,我在其中玩弄 fragment 。它包含一个 Activity (ComponentsActivity),在纵向 View 中附加一个 fragment (ComponentsFragment)。切换到横向时,ComponentsFragment 保留在屏幕左侧,ArticleFragment 添加到右侧。以纵向模式开始时一切正常,切换到横向模式也正常,但在处于横向模式后尝试切换回纵向模式会导致 ArticleFragment 的 OnActivityCreated 方法中出现空指针异常。我不确定为什么要调用此方法,因为 Activity 应该使用单个 ComponentsFragment 重新创建自身。
这是 Activity 代码:
package com.cab.apitesting;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
public class ComponentsActivity extends FragmentActivity implements ComponentsFragment.OnArticleSelectedListener {
boolean mDualPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_components);
View articleContainer = findViewById(R.id.article_container);
mDualPane = articleContainer != null && articleContainer.getVisibility() == View.VISIBLE;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, ComponentsFragment.newInstance());
transaction.commit();
if (mDualPane) {
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.add(R.id.article_container, ArticleFragment.newInstance(1));
transaction2.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction2.addToBackStack(null);
transaction2.commit();
}
}
@Override
public void onArticleSelected(int id) {
int articleIndex = 0;
switch (id) {
case R.id.button_article_one:
articleIndex = 1;
break;
case R.id.button_article_two:
articleIndex = 2;
break;
case R.id.button_article_three:
articleIndex = 3;
break;
default:
break;
}
if (mDualPane) {
FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
transaction3.add(R.id.article_container, ArticleFragment.newInstance(articleIndex));
transaction3.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction3.commit();
} else {
FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
transaction3.replace(R.id.fragment_container, ArticleFragment.newInstance(articleIndex));
transaction3.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction3.addToBackStack(null);
transaction3.commit();
}
}
}
这是 ComponentsFragment 代码:
package com.cab.apitesting;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class ComponentsFragment extends Fragment {
Button artOneButton;
Button artTwoButton;
Button artThreeButton;
OnArticleSelectedListener mArticleListener;
public static ComponentsFragment newInstance() {
ComponentsFragment f = new ComponentsFragment();
Bundle args = new Bundle();
f.setArguments(args);
return f;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mArticleListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_components, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
artOneButton = (Button)getView().findViewById(R.id.button_article_one);
artOneButton.setOnClickListener(onClick);
artTwoButton = (Button)getView().findViewById(R.id.button_article_two);
artTwoButton.setOnClickListener(onClick);
artThreeButton = (Button)getView().findViewById(R.id.button_article_three);
artThreeButton.setOnClickListener(onClick);
}
private View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.button_article_one:
case R.id.button_article_two:
case R.id.button_article_three:
mArticleListener.onArticleSelected(view.getId());
break;
default:
break;
}
}
};
public interface OnArticleSelectedListener {
public void onArticleSelected(int id);
}
}
这是 ArticleFragments 代码:
package com.cab.apitesting;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ArticleFragment extends Fragment {
private static String[] articles = {
"Bacon ipsum dolor sit amet ribeye nulla labore cow. Reprehenderit meatball pork belly non, laborum commodo tail. Aliquip nulla ad ham hock. Sausage swine jerky nulla consectetur in voluptate short loin ham hock tempor. Ball tip tenderloin excepteur strip steak in. Incididunt pork chop dolor, nisi short ribs ex mollit proident consequat.\n" +
"\n" +
"Velit sirloin doner brisket leberkas andouille elit rump. Cow est pariatur fugiat. Culpa shankle jowl andouille. Chuck beef ribs fatback ut, aliquip duis doner aute.\n" +
"\n" +
"T-bone consectetur filet mignon dolore beef ribs mollit, tenderloin esse tempor leberkas labore meatloaf ball tip. Jerky filet mignon meatloaf, officia pastrami voluptate beef drumstick capicola aute chuck shoulder venison short ribs. Fugiat ad enim venison, frankfurter rump beef ribs pancetta. Duis laborum in corned beef dolor in. Ball tip leberkas esse, pariatur commodo bacon jerky jowl pancetta bresaola tongue minim aliqua t-bone meatloaf. Ham sint tongue ut sunt pig culpa anim beef sirloin do. Irure jerky dolore tenderloin ball tip mollit ham hock, boudin eiusmod cow exercitation strip steak in filet mignon.\n" +
"\n" +
"Deserunt veniam meatloaf cupidatat meatball chicken sed qui cow consectetur filet mignon. Ad officia adipisicing, pig biltong nostrud nulla sirloin in sausage velit. Ut laborum meatloaf, tail exercitation consectetur officia fatback sirloin. Venison incididunt strip steak ham, sint ut pancetta cupidatat aliquip eiusmod in officia leberkas filet mignon. Beef ribs laborum kielbasa mollit boudin beef. Tri-tip ball tip pig nulla dolor ut pork chop aliqua corned beef sirloin t-bone. Ea adipisicing elit turkey velit id.\n" +
"\n" +
"Shank leberkas dolore pork chop nostrud qui short loin. Commodo ad ex aute proident elit filet mignon qui aliqua laborum kielbasa beef ribs ball tip labore. Voluptate laboris ut biltong incididunt corned beef. Ham hock kielbasa bresaola, shankle sint eu in reprehenderit minim excepteur frankfurter id sirloin commodo ut.",
"Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.\n"
};
public static ArticleFragment newInstance(int index) {
ArticleFragment f = new ArticleFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_article, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView article = (TextView)getActivity().findViewById(R.id.textview_article);
int index = getArguments().getInt("index");
article.setText(articles[index - 1]);
}
}
错误日志如下:
08-20 15:35:58.214 1328-1328/com.cab.apitesting E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cab.apitesting/com.cab.apitesting.ComponentsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.cab.apitesting.ArticleFragment.onActivityCreated(ArticleFragment.java:62)
at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
... 12 more
导致错误的行是:
article.setText(articles[index - 1]);
在 ArticleFragment 类中。我假设这是失败的,因为 article 变量为 null(应该是因为当应用程序处于纵向模式时 ArticleFragment 不应该存在)。但是为什么在一个根本不应该存在的 fragment 中调用 OnActivityCreated 方法?
最佳答案
不是这一行..
TextView article = (TextView)getActivity().findViewById(R.id.textview_article);
替换为..
TextView article = (TextView)getView().findViewById(R.id.textview_article);
关于Fragment OnActivityCreated方法中的Android空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18339324/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
指针, C语言的精髓 莫队先咕几天, 容我先讲完树剖 (因为后面树上的东西好多都要用树剖求 LCA). 什么是指针 保存变量地址的变量叫做指针. 这是大概的定义, 但是Defad认为
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我是一名优秀的程序员,十分优秀!