2012-03-08 5 views
0

안녕하세요. 첫 번째 Android 앱을 시작하고 TextView가 잠시 후 단어를 변경하려고합니다.setText가 앱을 부순 것이다.

<?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의는 다음과 같습니다 :

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 파일입니다

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) 
:

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

내가 실행하려고하면 로그 캣가 나에게 말한다

setText가 문제를 일으키는 것 같습니다. 내가이 줄을 주석 처리했을 때 문제가 발생하지 않았다.

나는 아무것도 못 찾았나요?

답변

1

당신은하지 그 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() BEI라는 점이다. 이 시점에서 클래스 변수 gameTextnull입니다.

+0

음에 R.java에 어떤 값으로 문자열 값을 설정하고,이 할 일 버그를 해결하면, 새로운 것은 그것이 gameText가 항상 null이 될 것이고 if 문 안의 부분에 결코 도달하지 않을 것이라는 것입니다. gameText 초기화되지 않습니다, 어떻게 작동합니까 궁금해? 감사! – jl123

+0

클래스 변수'gameText'는'onCreate()'에 의해 초기화되지만 생성자'GameViews()'가 호출 된 후에 초기화됩니다. 나는 내 대답을 편집했다. 그렇지 않으면 무한 루프가 될 수있다. – alexvetter

관련 문제