2012-03-08 35 views
0

嗨我开始我的第一个Android应用程序,并试图让TextView改变了一段时间后的话。setText会粉碎应用程序

public class Game1Activity extends Activity { 

    GameViews ngv; 
    private static String gameId = "game1Activity-game"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game1_layout); 
     ngv = (GameViews)findViewById(R.id.Game1); 
     ngv.setTextView((TextView)findViewById(R.id.Text)); 
    }; 

} 
public class GameViews extends View 
{ 
    private TextView gameText; 
    private long time1 = System.currentTimeMillis(); 

    public GameViews(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     initGameViews(); 
    } 

    public GameViews(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     initGameViews(); 
    } 

    public void setTextView(TextView tv) 
    { 
     gameText = tv; 
    } 

    public void initGameViews() 
    { 
     setFocusable(true); 
     Resources r = this.getContext().getResources(); 
     changeGameText(); 
    } 

    public void changeGameText() 
    { 
     while (true) 
     { 
      if (System.currentTimeMillis() - time1 > 100) 
      { 
       gameText.setText(R.string.hi2); 
       return; 
      } 
     } 
    } 
} 

布局XML文件是:

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <game1.code.GameViews 
    android:id="@+id/Game1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    tileSize="24" /> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" > 

     <TextView 
     android:id="@+id/Text" 
     android:text="@string/hi1" 
     android:visibility="visible" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:gravity="center_horizontal" 
     android:textColor="#ff8888ff" 
     android:textSize="24sp"/> 
    </RelativeLayout> 
</FrameLayout> 

和的strings.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hi1">hi1</string> 
    <string name="hi2">hi2</string> 
</resources> 

当我试图运行时,logcat的告诉我说:

03-08 03:19:28.067: E/AndroidRuntime(602): Caused by: java.lang.NullPointerException 
03-08 03:19:28.067: E/AndroidRuntime(602): game1.code.GameViews.changeGameText(GameViews.java:67) 
03-08 03:19:28.067: E/AndroidRuntime(602): 
game1.code.GameViews.initGameViews(GameViews.java:52) 

看来,setText导致了问题,一个当我评论这条线时,没有发生问题。

我错过了什么吗?

回答

1

您的字符串值设置为在R.java一些价值不是由R.java变量

更改参考值

gameText.setText(R.string.hi2); 

gameText.setText(getContext().getResources().getString(R.string.hi2)); 
0

您应该在changeGameText()方法中执行null检查。

if (System.currentTimeMillis() - time1 > 100 && gameText != null) 
{ 
    gameText.setText(R.string.hi2); 
    return; 
} 
else 
{ 
    return; 
} 

问题是,这changeGameText()initGameViews()调用,此方法被称为贝构造GameViews()。此时类变量gameTextnull

+0

好了,做这解决bug,新的东西是,它似乎gameText将永远是空的,将永远不会到达声明部分内部。 gameText从未初始化,我想知道这是如何工作的?谢谢! – jl123 2012-03-08 08:42:36

+0

类变量'gameText'由'onCreate()'初始化,但是在调用构造函数GameViews()之后。我编辑了我的答案,否则你可能会陷入无限循环。 – alexvetter 2012-03-08 09:01:09