简体   繁体   English

Android SetText空指针异常

[英]Android SetText Null pointer Exception

I've just begin Android development and I'm sure you can help with this (I'm sorry about my bad English) 我刚刚开始Android开发,并且我确定您可以为您提供帮助(对我英语不好的情况感到抱歉)

I have a main activity, and at a certain moment i want to call another activity, in wich a want to change a textview with some message. 我有一个主要活动,在某个时刻我想打电话给另一个活动,以改变一些消息的文本视图。 At this moment I get a Null pointer Exception if I dont put setContentView(R.layout.register); 此时,如果不放置setContentView(R.layout.register);,我将得到Null指针异常。

But when I put that line, I see for a milisecond correctly the Register activiy with my new text "Android2" and then jump again to a register activity with no text. 但是,当我把这行代码放进去时,我可以看到一个毫秒的正确时间,其中包括我的新文本“ Android2”的Register活动,然后再次跳转至无文本的注册活动。 I mean I draw it twice. 我的意思是我画了两次。 I hope I have explained enough. 我希望我已经解释够了。

The question is Where do I have to put setcontentview and with what layaout. 问题是我必须在哪里放置setcontentview以及进行什么布局。

Thank you very much, Daniel 非常感谢,丹尼尔

I show you some code: 我给你看一些代码:

My main activity has this method: 我的主要活动有这种方法:

  protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {

        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
        {
            Intent i = new Intent(this, register.class);
            startActivity(i);       

            setContentView(R.layout.register);
            TextView text = (TextView) findViewById(R.id.texto);

            try {
                text.setText("Android2");
                }catch(Exception e) {
                    Log.i("Log", e.getMessage()+"Error!"); // LogCat message
                }
        }
        //super.onActivityResult(requestCode, resultCode, data);
    }

My second activity class called register: 我的第二个活动类称为寄存器:

public class register extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.register);
   }
}

The register Interface is register.xml 注册接口是register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/about_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/register" />

    <TextView
        android:id="@+id/texto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/continue_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/save" />

        <Button
            android:id="@+id/new_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/repeat" />
    </LinearLayout>

</LinearLayout>

What you basically do is the following: 您的基本操作如下:

  1. you prepare an intent to start another activity 您准备开始另一项活动的意图
  2. you start the activity you prepared the intent for 您开始准备活动的活动
  3. you set the current activity's content to R.layout.register 您将当前活动的内容设置为R.layout.register
  4. you get the textView (on the current activity) 您将获得textView(在当前活动中)
  5. you set the text of the textView to Android2 您将textView的文本设置为Android2

And, at this moment the new activity appears on the screen. 并且,此时新的活动将出现在屏幕上。 Please note that your code is not correct, since you manipulate UI elements in the current activity and you expect changes on the newly started activity. 请注意,您的代码是不正确的,因为您在当前活动中操纵UI元素,并且期望对新启动的活动进行更改。

Move this code 移动此代码

TextView text = (TextView) findViewById(R.id.texto);

try {
    text.setText("Android2");
}catch(Exception e) {
    Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}

to the register activity's onCreate() method. 到注册活动的onCreate()方法。

BTW, usually, when you create a class, it's name should begin with a capital letter according to the standards. 顺便说一句,通常,当您创建一个类时,其名称应根据标准以大写字母开头。

You have two different activities. 您有两种不同的活动。 One of them is using the register.xml view, and the second one is trying to access the register view. 其中之一正在使用register.xml视图,第二个正在尝试访问寄存器视图。 The view only exists in your "register" activity. 该视图仅存在于您的"register"活动中。 The other activity seems to have no view? 其他活动似乎没有意见? That's probably why you're getting NULL . 这可能就是为什么您得到NULL

You should merge those two classes together, since it looks like you're trying to access texto from the same view. 您应该将这两个类合并在一起,因为看起来您正在尝试从同一视图访问texto

So, to summarise, findViewById should be called from within the activity that calls setContentView . 因此,总而言之,应该从调用setContentView的活动中调用findViewById

Remove this line and it should work 删除此行,它应该工作

   startActivity(i); 

Not sure why you are calling that as an external activity. 不知道为什么将其称为外部活动。

Otherwise move below code to your register class 否则,将下面的代码移至您的注册类

setContentView(R.layout.register);
        TextView text = (TextView) findViewById(R.id.texto);

        try {
            text.setText("Android2");
            }catch(Exception e) {
                Log.i("Log", e.getMessage()+"Error!"); // LogCat message
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM