마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

onListItemClick 클릭 시 앱이 죽습니다.

0 추천
 
MainActivity에서 onListItemClick 에서 스위치를 사용하여 position 마다 옵션을 주려고 했는데 앱이 바로 죽습니다.
 
--------------row_layout.xml--------------------
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/listText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textColor="#ff00ff"
        android:textSize="18sp"
        android:textStyle="bold" />
 
</LinearLayout>
 
-----------activity_main.xml---------------------
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/mainText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My list" />
 
    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainText"
        android:background="#aaaaaa" />
 
    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mainText"
        android:text="There is no data"
        android:textStyle="bold" />
 
</RelativeLayout>
 
---------------------MainActivity.java----------------------
package com.example.list;
 
import java.util.*;
 
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
 
public class MainActivity extends ListActivity {
    private TextView text;
    private List<String> listValues;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        text = (TextView) findViewById(R.id.mainText);
        
        listValues = new ArrayList<String>();
        listValues.add("Android");
        listValues.add("iOS");
        listValues.add("Symbian");
        listValues.add("Blackberry");
        listValues.add("Windows Phone");
        // initiate the listadapter
        ArrayAdapter<String> myAdapter = new ArrayAdapter <String>(this,
                R.layout.row_layout, R.id.listText, listValues);
         // assign the list adapter
         setListAdapter(myAdapter);
    }
    // when an item of the list is clicked
    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);
//        String selectedItem = (String) getListView().getItemAtPosition(position);
        //String selectedItem = (String) getListAdapter().getItem(position);
        
        switch (position) {
case 0:
 
Toast.makeText(getApplicationContext(), position, Toast.LENGTH_LONG).show();
 
break;
 
case 1:
Toast.makeText(getApplicationContext(), position, Toast.LENGTH_LONG).show();
 
break;
 
 
default:
break;
}
        
    }
}
 
바밥 (420 포인트) 님이 2015년 5월 6일 질문

2개의 답변

0 추천
onListItemListener가 아니라 onItemClickListener 를 사용하세요
쎄미 (162,410 포인트) 님이 2015년 5월 6일 답변
0 추천
앱이 죽을 때 로그를 올려주세여
Gradler (109,780 포인트) 님이 2015년 5월 6일 답변
...