2011-08-28 4 views
1

데이터를 수신 할 때 증가하는 목록이 뒤 따르는 2 개의 버튼을 표시하는 GUI를 만들려고합니다. 이전에 TextView에 데이터를 추가했지만 ListView로 전환했지만 응용 프로그램이 손상되었습니다.Android ListView가 응용 프로그램을 닫습니다.

package com.test; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
public class MainActivity extends ListActivity implements OnClickListener 
{ 
    String[] packets; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button start =(Button)findViewById(R.id.start); 
     start.setOnClickListener(this); 
     Button stop =(Button)findViewById(R.id.stop); 
     stop.setOnClickListener(this); 
     setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item,packets)); 

    } 
    public void onClick(View arg0) { 
    switch(arg0.getId()){ 
    case R.id.start: 
         ((ArrayAdapter)getListAdapter()).add("hello"); 
      break; 
     } 
    } 
} 

내 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" 
    > 
    <Button android:id="@+id/start" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Start" 
    /> 
    <Button android:id="@+id/stop" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Stop" 
    /> 
    <ListView android:id="@+id/list" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    /> 
</LinearLayout> 

내 list_item.xml입니다 : 여기

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:textSize="16sp" > 
</TextView> 

내 AndroidManifest.xml에 있습니다 :

여기

내 코드입니다
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.test" 
     android:versionCode="1" 
     android:versionName="1.0"> 


    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".MainActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_SUPERUSER" /> 
</manifest> 
내가 시작을 클릭 안녕하세요 목록에 추가됩니다 것을 원하는

Thread [<1> main] (Suspended (exception RuntimeException)) 
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1630  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1646 
    ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 121 
    ActivityThread$H.handleMessage(Message) line: 936 
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 3652  
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
    Method.invoke(Object, Object...) line: 507 
    ZygoteInit$MethodAndArgsCaller.run() line: 862 
    ZygoteInit.main(String[]) line: 620 
    NativeStart.main(String[]) line: not available [native method] 

:

여기 내 스택 트레이스입니다.

내가 잘못하고있는 것을 파악할 수 없다. 제발 도와주세요.

+2

는이 문제에 대한 스택 추적 및/또는 로그 캣을 게시 할 수 :

String[] packets = new String[10]; 

또한, 당신은 당신의 레이아웃에있는 ListView의 ID를 변경해야합니다. – hooked82

+0

게시했습니다. 확인하십시오. – d34th4ck3r

답변

0

변수 packets은 초기화되지 않으므로 예외의 원인 일 수 있습니다. 적어도 응용 프로그램에 적합한 초기 용량을 가진 빈 배열로 선언해야합니다. 예를 들어 :

@android:id/list 
+0

이 문제가 도움이되지 않지만 응용 프로그램이 계속 충돌합니다. – d34th4ck3r

+0

코드에 표시되는 다른 문제점으로 답변을 업데이트했습니다. 시도 해봐. 작동하지 않으면 스택 추적 외에 예외 메시지를 추가하십시오. 또한 응용 프로그램이 충돌 할 때 자세히 설명하십시오. 예를 들어. 버튼을 클릭하거나 Activity가 표시되기 전에 발생합니까? – elevine

+0

여전히 작동하지 않으며 응용 프로그램이 시작될 때 충돌이 발생합니다. logcat에는 오류/예외가 표시되지 않지만 eclipse는 위의 스택 추적을 제공합니다. 나는 list_item.xml을 추가했다. – d34th4ck3r

관련 문제