2011-09-08 9 views
-1

다음 버튼을 클릭했을 때 textview에서 텍스트를 변경하고 싶습니다. 나는이 코드를 시도했지만 올바른 방향으로 갈 수 없었다. 그걸 고쳐 주시겠습니까?문제점 : TextView에 문자열 배열이 설정되었습니다.

public class StringTestActivity extends ListActivity { 
TextView t; 
int count=0; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

    Resources res = this.getResources(); 
    final String[] capital = res.getStringArray(R.array.Cap); 

     //ArrayAdapter<String> n=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,capital); 
     //setListAdapter(n); 
    Button next=(Button)findViewById(R.id.btnNext); 
    next.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if(count<capital.length-1) 
      t.setText(capital[count]); 
      count++; 
     } 

    }); 

} }

편집 : 나는이 오류 메시지에 직면했다. enter image description here

다시 편집 : main.xml에

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

    <TextView android:id="@+id/list" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:hint="text" /> 

    <Button android:text="Next" android:id="@+id/btnNext" 
android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button>   

</LinearLayout> 

고해상도/문자열/array.xml는

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="Cap"> 
    <item>A</item> 
    <item>B</item> 
    <item>C</item> 
    <item>D</item> 
    </string-array> 
</resources> 
+0

? u는 어떤 문제를 일으키는 지 자세히 설명해 줄 수 있습니까? –

+0

이 프로젝트를 실행할 때 위의 메시지가 표시되었습니다 (미안 .....). 어떤 일이든 내 코드가 틀렸다. –

+0

귀하의 logcat에 들어가는 오류 메시지를 알려주십시오. 그의 이미지가 아닙니다. –

답변

1

당신의 ListView를 사용하지 않는 경우 다음 활동하지 ListActivity를 구현합니다. 그래서

공용 클래스 StringTestActivity이 활동 확장이 변경하게 {

또한 수입 android.app.Activity에서 u는 잘못된 점점 r에 어떤

+0

.. 내 편집 된 질문을주의 깊게보십시오.당신이 내 문제를 이해할 수 있기를 바랍니다. –

+0

줄에 오류가 있습니다 >>> t = (TextView) findViewById (R.id.list); ** 목록 **. –

+0

나는 그것을 소름 끼쳤다. thanks –

1
@Override 
     public void onClick(View v) { 
      t.setText(capital[count]); 
      count++; 
      count=count%capital.length; 
     } 
+0

아니야 (http://chat.stackoverflow.com/rooms/3264/discussion-between-kshetri-horror-and-android-power) 고치다. 오류 그림 메시지를 참조하십시오. –

+0

사용 t = (TextView) findViewById (R.id.list); – Rasel

1
당신이 "에 의해 의미합니까 무엇 귀하의 코드를 좋아 보이는

는 수 오른쪽 방향을 얻지 못하니? " 예외로 인해 충돌이 발생합니까? 코드가 변경되지 않습니까? 당신의 버튼을 고려 그런데

, 당신은 이런 식으로 재 작성 할 수 있습니다, 다음 호출됩니다

Override 
    public void onClick(View v) { 
     count++; 
     if(count < capital.length) // no - 1 
     { 
      t.setText(capital[count]); 
     } else { 
      /* perhaps v.setEnabled(false); ? */ 
     } 
    } 
+0

나는이 capital.length를 이미 시험해 보았다. 좀 더 말해줘. –

+0

Force Close가 해당 코드에서 비롯 되었습니까? 그림 대신에 로그를 게시하는 방법은 무엇입니까? –

+0

09-08 12 : 25 : 21.420 : ERROR/AndroidRuntime (13400) : java.lang.RuntimeException : 활동을 시작할 수 없습니다. ComponentInfo {ab/abStringTestActivity} : java.lang.RuntimeException : 콘텐츠에 id 속성을 가진 ListView가 있어야합니다. 'android.R.id.list'입니다. –

2
Button btn1; 
String countires[]; 
int i=0; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.prob2); 

    btn1 = (Button) findViewById(R.id.prob2_btn1); 

    countires = getResources().getStringArray(R.array.country); 

    for (String string : countires) 
    { 
     Log.i("--: VALUE :--","string = "+string); 
    } 

    btn1.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View arg0) 
     { 
      // TODO Auto-generated method stub 
      String country = countires[i]; 
      btn1.setText(country); 
      i++; 
      if(i==countires.length) 
       i=0; 
     } 
    }); 
} 
+0

@AB, 감사합니다.이 솔루션을 도울 수 있습니다. –