【发布时间】:2015-12-15 05:04:33
【问题描述】:
我是 Android 编程新手。老实说,我一直在网上到处寻找答案。请帮忙。
这是一个典型的stackoverflow链接,并且最接近我想要实现的目标 Change TextView text
我的主要问题是。每当我想更改 TextView 对象的文本时,都会出现运行时错误,并且应用程序处于“强制关闭”状态。我有 C++ 和 PHP 的 OOP 经验。我根本不明白为什么应用程序在 textView.setText(message) 上终止。一切都已定义。我什至尝试过 textView.setText("Hello")。每当我删除此语句时,应用程序就会运行。
activity_main.xml(整个文件)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MyApp.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="MyApp.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String message = "hi there";
TextView textView = (TextView) findViewById(R.id.hello_world);
textView.setText("this is a test");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
} // if
} // onCreate
// SKIPPED A FEW CODE
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
} // onCreateView
} // PlaceholderFragment
LogCat
06-23 20:11:42.106: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-23 20:11:42.116: E/AndroidRuntime(332): FATAL EXCEPTION: main
06-23 20:11:42.116: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyApp/MyApp.MainActivity}: java.lang.NullPointerException
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.os.Looper.loop(Looper.java:123)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-23 20:11:42.116: E/AndroidRuntime(332): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 20:11:42.116: E/AndroidRuntime(332): at java.lang.reflect.Method.invoke(Method.java:507)
06-23 20:11:42.116: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 20:11:42.116: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 20:11:42.116: E/AndroidRuntime(332): at dalvik.system.NativeStart.main(Native Method)
06-23 20:11:42.116: E/AndroidRuntime(332): Caused by: java.lang.NullPointerException
06-23 20:11:42.116: E/AndroidRuntime(332): at MyApp.MainActivity.onCreate(MainActivity.java:30)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-23 20:11:42.116: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-23 20:11:42.116: E/AndroidRuntime(332): ... 11 more
06-23 20:11:44.826: I/Process(332): Sending signal. PID: 332 SIG: 9
什么是NullPointerException和未捕获的异常?
同样,当我删除 setText() 行时应用程序运行,包含时字节终止。
我做错了什么?
【问题讨论】:
-
首先,阅读:stackoverflow.com/questions/23353173/… 然后,编辑您的问题以解释您的堆栈跟踪向您显示的内容。另外,请说明您认为您在哪里使用了您在此处列出的 XML。
-
嗨。感谢您的链接:-) 我添加了 xml 和 java 文件的名称,以及 LogCat ...我也是 stackoverflow 的新手,我不熟悉布局。我以为你的回复是自动回复。对不起:-)
标签: android textview runtime settext