textView.setText(消息)上的运行时错误...为什么? :-(

时间:2014-06-22 23:43:05

标签: android textview runtime settext

我是Android编程的新手。老实说,我一直在寻找网上各处的答案。请帮忙。

这是典型的stackoverflow链接,与我想要实现的最接近 Change TextView text

我的主要问题是。每当我想要更改TextView对象的文本时,我都会收到运行时错误,并且应用程序“强制关闭”。我有使用C ++和PHP的OOP经验。我根本不明白为什么应用程序终止于textView.setText(消息)。一切都是定义的。我甚至尝试过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()行时运行,byt在包含时终止。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

您正在setContentView(R.layout.activity_main)中呼叫onCreate()。您尚未在问题中提供res/layout/activity_main.xml的内容。

但是,您的错误表明res/layout/activity_main.xml不包含android:id="@+id/hello_world"的小部件。

您的res/layout/fragment_main.xml文件有这样的小部件,但到目前为止,您在代码中粘贴的代码中没有使用该文件。

或者:

  • 将具有正确ID的窗口小部件添加到res/layout/activity_main.xml

  • 根据您尝试使用的位置TextView

  • res/layout/fragment_main.xml逻辑移至适当的位置