【问题标题】:Android SetText Null pointer ExceptionAndroid SetText空指针异常
【发布时间】:2012-04-22 08:33:33
【问题描述】:

我刚刚开始 Android 开发,我相信你可以帮助解决这个问题(我很抱歉我的英语不好)

我有一个主要活动,并且在某个时刻我想调用另一个活动,其中想要更改带有一些消息的文本视图。此时如果我不放,我会得到一个空指针异常 setContentView(R.layout.register);

但是当我输入该行时,我正确地看到了带有我的新文本“Android2”的注册活动一毫秒,然后再次跳转到没有文本的注册活动。我的意思是我画了两次。 我希望我已经解释得够多了。

问题是我必须在哪里放置 setcontentview 以及使用什么布局。

非常感谢你,丹尼尔

我给你看一些代码:

我的主要活动有这个方法:

  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);
    }

我的第二个活动类叫做注册:

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

注册接口是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>

【问题讨论】:

  • 你能在第一个活动中粘贴你的总代码吗?你有没有 onCreate

标签: android settext


【解决方案1】:

你基本上做的是以下几点:

  1. 您准备开始另一项活动的意图
  2. 您开始了您为其准备意图的活动
  3. 您将当前活动的内容设置为 R.layout.register
  4. 您将获得 textView(在当前活动上)
  5. 您将 textView 的文本设置为 Android2

而且,此时新的活动出现在屏幕上。请注意,您的代码不正确,因为您在当前活动中操作 UI 元素,并且您希望新启动的活动发生变化。

移动此代码

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

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

到注册活动的 onCreate() 方法。

顺便说一句,通常,当您创建一个类时,根据标准,它的名称应该以大写字母开头。

【讨论】:

  • 非常感谢您的帮助。我真的很喜欢它。丹尼尔。
  • 如果您认为这是答案,也请将此作为答案。谢谢
【解决方案2】:

您有两种不同的活动。其中之一是使用 register.xml 视图,第二个是尝试访问 register 视图。该视图仅存在于您的 "register" 活动中。其他activity好像没有view?这可能就是您收到NULL 的原因。

您应该将这两个类合并在一起,因为看起来您正试图从同一视图访问texto

因此,总而言之,应该从调用setContentView 的活动中调用findViewById

【讨论】:

  • 非常感谢您的帮助。我真的很喜欢它。丹尼尔。
【解决方案3】:

删除这一行,它应该可以工作

   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
            }
    }

【讨论】:

  • 太棒了!它有效,现在我想知道我是否将那段代码移动到其他活动中,我如何传递消息的值,现在是我只在我的主类中知道的测试文本“Android2”?谢谢丹尼尔。
  • 哦,我要谢谢你,所以setContentView 必须高于一切
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2018-01-19
  • 2013-04-16
  • 1970-01-01
相关资源
最近更新 更多