2013-07-26 3 views
-1

이 코드에서 무엇이 잘못되었는지 알아내는 데 도움이 필요합니다. ListView OnItemClickListeners를 구현하는 방법을 검색했지만 제대로 작동하지 않습니다.안드로이드, 응용 프로그램 충돌, ListView Listener

응용 프로그램은 OnItemClickListeners를 설정하는 메서드없이 잘 실행됩니다. 설정하면 응용 프로그램이 다운됩니다.

이 액티비티의 목적은 회 전자에 해당하는 클라이언트 (회 전자에 다른 도시 포함)를로드하고 항목을 클릭 할 때마다 다른 활동을 시작하는 것입니다. 여기

public class MainActivity extends Activity { 

    private Spinner spinner2; 
    private Button sync; 
    private static String selectedOption = ""; 
    private ListView listview; 
    private static ArrayList<String> values; 
    private static Client[] clientArray; 

    final Context context = this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     sync = (Button) findViewById(R.id.bSync); 

     //Client list 
     clientArray = new Client[5]; 

     //Populate the client list 
     clientArray[0] = new Client("Roberto Herrero", "Resistencia", "Calle Falsa 123", 10.0f); 
     clientArray[1] = new Client("Carlos Stigeslts", "Resistencia", "Calle Falsa 123", 10.0f); 
     clientArray[2] = new Client("Pedro Zaragoza", "Resistencia", "Calle Falsa 123", 10.0f); 
     clientArray[3] = new Client("Pablo Pedroso", "Charata", "Calle Falsa 123", 10.0f); 
     clientArray[4] = new Client("Juan Jose Falalalala", "Resistencia", "Calle Falsa 123", 10.0f); 

     spinner2 = (Spinner) findViewById(R.id.spinner2); 
     List<String> list = new ArrayList<String>(); 

     int count = 0; 

     if(clientArray != null && clientArray.length != 0) { 
      String local = clientArray[0].getLocality(); 
      list.add(local); 
      for(int i = 0; i < clientArray.length; i++) { 
       if(!local.equals(clientArray[i].getLocality())) { 
        if(!list.contains(clientArray[i].getLocality())) { 
         list.add(clientArray[i].getLocality()); 
        } 
       } 
      } 
     } 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, list); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner2.setAdapter(dataAdapter); 

     addSpinnerListener(); 
     addButtonListeners(); 

     //THIS IS CAUSING THE APP TO CRASH, WITHOUT THIS METHOD CALL, THE APPS RUNS FINE 
     setListviewListeners(); 

    } 

    public void setListviewListeners() { 
     listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Intent intent = new Intent("android.intent.action.CLIENTVIEW"); 
       startActivity(intent); 
      } 
     }); 
    } 

    public void addSpinnerListener() { 
     spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long l) { 
       String selected= parent.getItemAtPosition(position).toString(); 
       selectedOption = selected; 

       //List Values 
       values = new ArrayList<String>(); 

       for(int i = 0; i < clientArray.length; i++) { 

        if(clientArray[i].getLocality().equalsIgnoreCase(selectedOption)) { 
         values.add(clientArray[i].getName() + "\n" + clientArray[i].getAddress()); 
        } 
       } 

       //Declare, Define and find the ListView 
       listview = (ListView) findViewById(R.id.listview); 

       ArrayList<String> vlist = new ArrayList<String>(); 
       for (int i = 0; i < values.size(); ++i) { 
        vlist.add(values.get(i)); 
       } 

       ArrayAdapter adapter = new ArrayAdapter(context, 
         android.R.layout.simple_list_item_1, vlist); 

       listview.setAdapter(adapter); 

      } 

      @Override 
      public void onNothingSelected(AdapterView<?> adapterView) { 

      } 
     }); 
    } 

    public void addButtonListeners() { 
     sync.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

       // set title 
       alertDialogBuilder.setTitle("REPORT"); 

       // set dialog message 
       alertDialogBuilder.setMessage("NOT IMPLEMENTED"); 
       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
      } 
     }); 
    } 
} 

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

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



    <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:baselineAligned="false" 
     android:weightSum="100"> 

     <TableRow 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="5dip"> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="80" 
        android:text="Nombre" 
        android:id="@+id/textView" 
        android:layout_column="0"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="15" 
        android:text="Saldo" 
        android:id="@+id/textView2" 
        android:layout_column="1"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="5" 
        android:text="Cascos" 
        android:id="@+id/textView3" 
        android:layout_column="2"/> 
     </TableRow> 

    <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="80" 
       android:text="Saldo Anterior" 
       android:id="@+id/textView" 
       android:layout_column="0"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="15" 
       android:text="SaldoA" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:text="CascosA" 
       android:id="@+id/textView3" 
       android:layout_column="2"/> 
    </TableRow> 

    <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="80" 
       android:text="Retiros" 
       android:id="@+id/textView" 
       android:layout_column="0"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="15" 
       android:text="SaldoA" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 

     <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:inputType="number" 
       android:ems="2" 
       android:id="@+id/editText"/> 
    </TableRow> 

    <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="80" 
       android:text="Entrega" 
       android:id="@+id/textView" 
       android:layout_column="0"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="15" 
       android:text="Entrega" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 

     <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:inputType="number" 
       android:ems="2" 
       android:id="@+id/editText"/> 
    </TableRow> 

    <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="80" 
       android:text="Pagos" 
       android:id="@+id/textView" 
       android:layout_column="0"/> 

     <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="15" 
       android:inputType="number" 
       android:ems="2" 
       android:id="@+id/editText"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:text="" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 
    </TableRow> 

    <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dip"> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="80" 
       android:text="Pagos" 
       android:id="@+id/textView" 
       android:layout_column="0"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="15" 
       android:text="SaldoAc" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="5" 
       android:text="CascosAc" 
       android:id="@+id/textView2" 
       android:layout_column="1"/> 
    </TableRow> 

이것이의 JAVA되는 XML

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

    <Button 
      android:id="@+id/bSync" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="SINCRONIZAR" /> 

    <Spinner 
      android:id="@+id/spinner2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/listview" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"/> 

</LinearLayout> 

는이 난 시작할 활동 매니페스트

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.sistel.listview" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="8" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.sistel.listview.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> 

     <activity 
       android:name="com.sistel.listview.ClientView" 
       android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.CLIENTVIEW" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

    </application> 

이다 시작하려는 활동

public class ClientView extends Activity { 

    private Button update; 
    private Context context = this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.client); 

     initComponets(); 
     buttonListener(); 

    } 

    public void initComponets() { 
     update = (Button) findViewById(R.id.bUpdate); 
    } 

    public void buttonListener() { 
     update.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

       // set title 
       alertDialogBuilder.setTitle("REPORT"); 

       // set dialog message 
       alertDialogBuilder.setMessage("DUMMY PROCEDURE"); 
       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
      } 
     }); 

    } 

} 
+0

앱이 충돌하는 경우 LogCat 스택 추적을 포함 시키십시오. 항상 문제가되는 라인이 있으므로 LogCat 스택 추적을 포함하십시오. – tolgap

+0

목록 항목을 클릭하면 충돌이 발생합니까? 또는, 전에도 기회가 있습니다. – Vikram

답변

2

내가 틀렸다면 정정 해주세요. 그러나 결코 초기화하지 않는 것 같습니다. listview. 여기

private ListView listview; 

를 선언하지만 초기화 것처럼 당신이 그것에 listener을 설정하려고 적어도 전에이 보이지 않는다. 이것이 문제가 아니라면 문제가 무엇인지 정확히 알 수 있도록 logcat을 게시하십시오.

+0

Gabriel Matusevich가이를 수행합니다. 제 생각에는 올바른 방식이 아닙니다. 'setListviewListener()'전에 호출되는'addSpinnerListener()'를 보라. – Vikram

+0

OMgg, 그랬지, 네, 목록은 methor 내부에서 초기화되었지만 onCreate에서는 초기화되지 않았습니다 ... 문제가 수정되었습니다 –

+0

@GabrielMatusevich 답변 옆에있는 체크 표시를 클릭하여 정답으로 수락 할 수 있습니다 if 그것은 당신을 위해 일했다. – codeMagic

관련 문제