android setText崩溃我的应用程序

时间:2014-04-18 10:53:26

标签: android android-fragments textview

我在Android 16(4.1)应用上使用setText()方法时遇到问题。我试图以编程方式将任何内容放入TextView tvopis(这里只是一些测试字符串,后来我计划将意图放入字符串)。 当我打开活动opis时,应用程序崩溃了。如果我评论setText()部分,一切正常。

这是java代码

package com.example.pierwszyandroid;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class OpisActivity extends Activity {

    public TextView tvopis;
    String txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opis);
         tvopis = (TextView) findViewById(R.id.tvotest);

         tvopis.setText("txt test");


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

这是此活动的fragment_opis.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.pierwszyandroid.OpisActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/tvotest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

这是activity_opis.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="com.example.pierwszyandroid.OpisActivity"
    tools:ignore="MergeRootFrame" />

这里是带有错误消息的logcat:

04-18 10:44:59.665: I/Choreographer(723): Skipped 206 frames!  The application may be doing too much work on its main thread.
04-18 10:46:34.016: I/Choreographer(723): Skipped 41 frames!  The application may be doing too much work on its main thread.
04-18 10:46:34.135: D/AndroidRuntime(723): Shutting down VM
04-18 10:46:34.135: W/dalvikvm(723): threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
04-18 10:46:34.155: E/AndroidRuntime(723): FATAL EXCEPTION: main
04-18 10:46:34.155: E/AndroidRuntime(723): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pierwszyandroid/com.example.pierwszyandroid.OpisActivity}: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:137)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 10:46:34.155: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 10:46:34.155: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)
04-18 10:46:34.155: E/AndroidRuntime(723): Caused by: java.lang.NullPointerException
04-18 10:46:34.155: E/AndroidRuntime(723):  at com.example.pierwszyandroid.OpisActivity.onCreate(OpisActivity.java:25)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.Activity.performCreate(Activity.java:5008)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-18 10:46:34.155: E/AndroidRuntime(723):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-18 10:46:34.155: E/AndroidRuntime(723):  ... 11 more

任何帮助都会非常感激,因为我找不到已经发布的类似问题的答案。

5 个答案:

答案 0 :(得分:1)

如果查看日志cat,当您尝试将文本设置为文本视图时(OpisActivity中的第25行),您将看到有一个空指针异常。

这意味着在布局中找不到txt视图,因此无法初始化。由于textview位于片段中,因此必须先将片段添加到布局中,然后找到视图。

要解决您的问题,请在onCreate方法中进行片段初始化后移动findById和setText方法,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opis);

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

     tvopis = (TextView) findViewById(R.id.tvotest);
     tvopis.setText("txt test");
}

答案 1 :(得分:1)

您的应用已崩溃,因为您的TextView tvotest属于 fragment_opis.xml 布局,您必须将activity_opis.xml 文件设置为您的活动。

首先初始化PlaceholderFragment()然后初始化您的TextView tvotest或更改您的布局

setContentView(R.layout.fragment_opis.xm)

答案 2 :(得分:0)

您在onCreate()方法中错误地设置了布局。您TextView位于布局fragment_opis.xml内,而您将布局设置为setContentView(R.layout.activity_opis);

只需更改setContentView(R.layout.activity_opis);

即可

如下

  setContentView(R.layout. fragment_opis);

答案 3 :(得分:0)

更改此setContentView(R.layout.activity_opis);

到这个

setContentView(R.layout.fragment_opis.xml);

答案 4 :(得分:0)

有时在片段中创建布局时,您将粘贴并粘贴具有重复ID的xml代码。然后,当您调用粘贴的资源的ID时,它将尝试调用第一个定义的资源,该资源在运行时可能为null。仔细检查是否仅在一个片段的Xml中找到了TextView的ID。如果不能解决“ findviewbyid”问题,希望对您有帮助