抱歉,这可能是一个简单的错误,对Android开发人员来说是新的,但是我一直在网上寻找答案,而这些解决方案似乎都不起作用。每当我尝试运行下面的代码时,都会提示为什么/如何修复它的任何想法。首先是java文件
public class Services extends Activity { Report2 r; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle data = getIntent().getExtras(); r = data.getParcelable("RemarksReport"); setContentView(R.layout.activity_services); TextView sampleText = (TextView) findViewById(R.id.services_text);
一直工作到这里,然后在下一行崩溃
sampleText.setText("ANYTHING"); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } }
XML文件中的部分看起来像这样,
<TextView android:id="@+id/services_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="42px" android:text="@string/sample_services" />
它在表格中的位置,呈线性布局。
Logcat错误:
07-01 22:45:09.952: E/AndroidRuntime(8854): FATAL EXCEPTION: main 07-01 22:45:09.952: E/AndroidRuntime(8854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jasc/com.example.jasc.Services}: java.lang.NullPointerException 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread.access$600(ActivityThread.java:127) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.os.Handler.dispatchMessage(Handler.java:99) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.os.Looper.loop(Looper.java:137) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread.main(ActivityThread.java:4441) 07-01 22:45:09.952: E/AndroidRuntime(8854): at java.lang.reflect.Method.invokeNative(Native Method) 07-01 22:45:09.952: E/AndroidRuntime(8854): at java.lang.reflect.Method.invoke(Method.java:511) 07-01 22:45:09.952: E/AndroidRuntime(8854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 07-01 22:45:09.952: E/AndroidRuntime(8854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 07-01 22:45:09.952: E/AndroidRuntime(8854): at dalvik.system.NativeStart.main(Native Method) 07-01 22:45:09.952: E/AndroidRuntime(8854): Caused by: java.lang.NullPointerException 07-01 22:45:09.952: E/AndroidRuntime(8854): at com.example.jasc.Services.onCreate(Services.java:30) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.Activity.performCreate(Activity.java:4465) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 07-01 22:45:09.952: E/AndroidRuntime(8854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
在片段的内部onCreateView更改如下:
onCreateView
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.place_holder_fragment, container, false); servicesTxtView= (TextView) view.findViewById(R.id.services_text); servicesTxtView.setText("ANYTHING"); return view; }
您尝试不使用其容器视图来初始化textview。因此,您在调用setText它时会得到Null Pointer Exception 。
setText