在视图中更改文本时,android活动正在崩溃

时间:2014-06-06 18:42:25

标签: java android

我正在尝试做一些非常基本的事情,我想我犯了一个我无法看到的错误。我只想更改hello_world生成的android项目中的文本 这是我的代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView helloWorld = (TextView)findViewById(R.id.viewName);

        if(helloWorld != null){
            String str =(String) helloWorld.getText();
            Log.d("ViewText",str);
            helloWorld.setText(seq);
        }

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

这是我的UI的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="com.example.strreview.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/viewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bla" />

</RelativeLayout>

问题是应用程序甚至无法运行,当我尝试更改文本时崩溃。所以如果有人在这里看到问题,那将会很有帮助。我已经检查了其他类似问题,但我看不出有什么区别。

//// Stack Trace ////

  

06-06 09:46:18.061:E / MoreInfoHPW_ViewGroup(30784):父视图不是   一个TextView 06-06 09:46:18.081:D / AndroidRuntime(30784):关机   VM 06-06 09:46:18.081:W / dalvikvm(30784):threadid = 1:线程退出   未捕获的异常(组= 0x41707c08)06-06 09:46:18.081:   E / AndroidRuntime(30784):致命异常:主06-06 09:46:18.081:   E / AndroidRuntime(30784):进程:com.example.strreview,PID:30784   06-06 09:46:18.081:E / AndroidRuntime(30784):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.strreview / com.example.strreview.MainActivity}:   java.lang.NullPointerException 06-06 09:46:18.081:   E / AndroidRuntime(30784):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   android.app.ActivityThread.access $ 900(ActivityThread.java:169)06-06   09:46:18.081:E / AndroidRuntime(30784):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1277)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   android.os.Handler.dispatchMessage(Handler.java:102)06-06   09:46:18.081:E / AndroidRuntime(30784):at   android.os.Looper.loop(Looper.java:136)06-06 09:46:18.081:   E / AndroidRuntime(30784):at   android.app.ActivityThread.main(ActivityThread.java:5476)06-06   09:46:18.081:E / AndroidRuntime(30784):at   java.lang.reflect.Method.invokeNative(Native Method)06-06   09:46:18.081:E / AndroidRuntime(30784):at   java.lang.reflect.Method.invoke(Method.java:515)06-06 09:46:18.081:   E / AndroidRuntime(30784):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)06-06   09:46:18.081:E / AndroidRuntime(30784):at   dalvik.system.NativeStart.main(Native Method)06-06 09:46:18.081:   E / AndroidRuntime(30784):引起:java.lang.NullPointerException   06-06 09:46:18.081:E / AndroidRuntime(30784):at   com.example.strreview.MainActivity.onCreate(MainActivity.java:38)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   android.app.Activity.performCreate(Activity.java:5473)06-06   09:46:18.081:E / AndroidRuntime(30784):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)   06-06 09:46:18.081:E / AndroidRuntime(30784):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)   06-06 09:46:18.081:E / AndroidRuntime(30784):... 11更多06-06   09:46:18.096:I / Process(30784):发送信号。 PID:30784 SIG:9

2 个答案:

答案 0 :(得分:4)

我认为那是因为您的TextView不在xml:activity_main中,您应该将TexView添加到此XML中。

答案 1 :(得分:1)

TextView Parent view is not a TextView存在问题,没有TextView,您正试图访问它。所以它给予java.lang.NullPointerException