2012-04-22 103 views
2

我刚刚开始Android开发,我相信你可以帮忙(对不起我的英文很抱歉)Android SetText空指针异常

我有一个主要活动,并在某个时刻我想打电话给另一个活动,在这个活动中想要用一些消息来改变一个文本视图。在这一刻,我得到一个空指针异常,如果我不把 setContentView(R.layout.register);

但是,当我把这一行,我看到一个毫秒正确的注册活动与我的新文本“Android2”,然后再跳到没有文字的注册活动。我的意思是我画了两次。 我希望我已经解释够了。

问题是我在哪里必须把setcontentview和什么layaout。

非常感谢你,丹尼尔

我告诉你一些代码:

我的主要活动有以下方法:

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> 
+0

你可以在第一个活动中粘贴你的总代码吗你有没有创建 – 2012-04-22 08:37:50

回答

1

你有两个不同的活动。其中之一是使用register.xml视图,第二个是试图访问注册视图。该视图仅存在于您的"register"活动中。其他活动似乎没有看法?这可能是你得到NULL的原因。

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

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

+0

非常感谢你的帮助。我真的很喜欢它。丹尼尔。 – daniel 2012-04-22 19:25:49

0

删除此行,它应该工作

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

太棒了!它的工作原理,现在我想知道我是否将这段代码移动到其他活动中,我如何传递消息的价值,现在是我在主要类中只知道的测试文本“Android2”?谢谢丹尼尔。 – daniel 2012-04-22 19:20:02

+0

哦,我不得不感谢你,所以'setContentView'必须高于一切几乎 – Master345 2014-04-18 15:13:04

4

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

  1. 您准备的意图,开始另一个活动
  2. 启动活动你准备意图为
  3. 您将当前活动的内容设置为R.layout。注册
  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()方法。

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

+1

非常感谢你的帮助。我真的很喜欢它。丹尼尔。 – daniel 2012-04-22 19:26:22

+0

你能把我的答案投票吗?谢谢 – overbet13 2012-04-22 19:47:51

+0

如果您认为这是答案,请检查此答案。谢谢 – overbet13 2012-04-23 13:07:21