interException异常上的TextView的setTe

interException异常上的TextView的setTe

本文介绍了NullPointerException异常上的TextView的setText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新到Android。这是一对夫妇加入到在Eclipse ADT引用的MyFirstApp引导线。我只是想改变一个TextView的文本。我很尴尬,我一直在这几个小时,现在仍然不能找到一个解决方案。任何帮助AP preciated: - )

MainActivity.Java

 公共类MainActivity扩展ActionBarActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        TextView的objTV =(TextView中)this.findViewById(R.id.tv_id);
        objTV.setText(测试);        如果(savedInstanceState == NULL){
            getSupportFragmentManager()调用BeginTransaction()
                    。新增(R.id.container,新PlaceholderFragment())提交()。
        }
    }
...
}

fragment_main.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=$ com.example.myfirstapp.MainActivity PlaceholderFragment><的TextView
    机器人:ID =@ + ID / tv_id
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=@字符串/参考hello world/>< / RelativeLayout的>

LogCat中

  03-22 13:44:36.793:E / AndroidRuntime(1710):致命异常:主要
03-22 13:44:36.793:E / AndroidRuntime(1710):工艺:com.example.myfirstapp,PID:1710
03-22 13:44:36.793:E / AndroidRuntime(1710):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myfirstapp / com.example.myfirstapp.MainActivity}:显示java.lang.NullPointerException
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.os.Handler.dispatchMessage(Handler.java:102)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.os.Looper.loop(Looper.java:136)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread.main(ActivityThread.java:5017)
03-22 13:44:36.793:E / AndroidRuntime(1710):在java.lang.reflect.Method.invokeNative(本机方法)
03-22 13:44:36.793:E / AndroidRuntime(1710):在java.lang.reflect.Method.invoke(Method.java:515)
03-22 13:44:36.793:E / AndroidRuntime(1710):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
03-22 13:44:36.793:E / AndroidRuntime(1710):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-22 13:44:36.793:E / AndroidRuntime(1710):在dalvik.system.NativeStart.main(本机方法)
03-22 13:44:36.793:E / AndroidRuntime(1710):因:显示java.lang.NullPointerException
03-22 13:44:36.793:E / AndroidRuntime(1710):在com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:21)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.Activity.performCreate(Activity.java:5231)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-22 13:44:36.793:E / AndroidRuntime(1710):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-22 13:44:36.793:E / AndroidRuntime(1710):11 ...更多


解决方案

的TextView 是布局 fragment_main ,但你只充气 activity_main 的setContentView()。你想找到的观点是不是在活动视图层次。

可能的解决方案:


  • 移动的TextView 将活动的布局。


  • 移动code访问的TextView 来片段的 onCreateView()


I'm very new to Android. This is a couple of lines added to the MyFirstApp guide referenced in the Eclipse ADT. I'm just trying to change the text in a TextView. I'm embarrassed that I've been at this for hours now and still can't find a solution. Any help is appreciated :-)

MainActivity.Java

public class MainActivity extends ActionBarActivity {

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

        TextView objTV = (TextView)this.findViewById(R.id.tv_id);
        objTV.setText("test");

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

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="com.example.myfirstapp.MainActivity$PlaceholderFragment" >

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

</RelativeLayout>

LogCat

03-22 13:44:36.793: E/AndroidRuntime(1710): FATAL EXCEPTION: main
03-22 13:44:36.793: E/AndroidRuntime(1710): Process: com.example.myfirstapp, PID: 1710
03-22 13:44:36.793: E/AndroidRuntime(1710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.os.Looper.loop(Looper.java:136)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invoke(Method.java:515)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at dalvik.system.NativeStart.main(Native Method)
03-22 13:44:36.793: E/AndroidRuntime(1710): Caused by: java.lang.NullPointerException
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:21)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.Activity.performCreate(Activity.java:5231)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-22 13:44:36.793: E/AndroidRuntime(1710):     ... 11 more
解决方案

The TextView is in layout fragment_main but you've only inflated activity_main with setContentView(). The view you're trying to find is not in the activity view hierarchy.

Possible solutions:

  • Move the TextView to your activity layout.

  • Move the code accessing the TextView to the fragment's onCreateView().

这篇关于NullPointerException异常上的TextView的setText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:59