2013-01-06 3 views
0

음식점 목록을 표시하는 활동이 있습니다. 목록의 각보기에는 숫자 피커가 연결되어 있습니다. 응용 프로그램이 잘 실행되지만 목록을 표시하는 활동을 시작할 때 충돌이 발생합니다. ...숫자 피커로 인해 응용 프로그램이 충돌 함

이것은 내 클래스 파일입니다.

NumberPicker np; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_starters); 
    createTestData(); 

    adapter = new MenuItemArrayAdapter(this, starters); 
    this.setListAdapter(adapter); 

    np = (NumberPicker)findViewById(R.id.numpick); 
    np.setMinValue(0); 
    np.setMaxValue(99); 
} 

이것은 StartersActivity 클래스 용 my xml 레이아웃 파일입니다.

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="670dp"> 
</ListView> 

<TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/empty" /> 

<include 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    layout="@layout/pricebar" /> 

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

이 목록의 행에 대한 내 XML 레이아웃 파일입니다.

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

<NumberPicker 
    android:id="@+id/numpick" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" /> 

<ImageView 
    android:id="@+id/dishpic" 
    android:layout_width="140dp" 
    android:layout_height="150dp" 
    android:layout_alignBottom="@+id/numpick" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/item_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/reviewBtn" 
    android:layout_alignParentTop="true" 
    android:layout_alignRight="@+id/item_price" 
    android:text="ITEM NAME" 
    android:textSize="20sp" /> 

<TextView 
    android:id="@+id/item_price" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/reviewBtn" 
    android:layout_below="@+id/item_name" 
    android:layout_marginTop="17dp" 
    android:layout_toRightOf="@+id/dishpic" 
    android:text="ITEM PRICE" 
    android:textSize="20sp" /> 

<Button 
    android:id="@+id/reviewBtn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/dishpic" 
    android:layout_toLeftOf="@+id/numpick" 
    android:layout_toRightOf="@+id/dishpic" 
    android:text="@string/reviewBtn" /> 
내가 그것을 작동하는 다음 코드를 꺼낼 때 때문에 문제의 원인이되는 번호 따는 것 같다

.

np = (NumberPicker)findViewById(R.id.numpick); 
    np.setMinValue(0); 
    np.setMaxValue(99); 

어떤 문제 일 수 있습니까?

java.lang.RuntimeException가 : 시작할 수 없습니다 활동 ComponentInfo {com.example.waitron5/com.example.waitron5.StartersActivity} : java.lang.NullPointerException이

오류는 널 포인터 예외 목록이로드되기 전에 번호 선택기에 액세스하려는 것과 관련이 있다는 느낌이 들었습니다.

np = (NumberPicker)findViewById(R.id.numpick); 
np.setMinValue(0); 
np.setMaxValue(99); 
+0

게시자가 게시 한 레이아웃 파일이'R.layout.activity_starters'가 아니라는 점을 알게 되었습니까? – Henry

+0

죄송합니다. 레이아웃 파일은 목록에있는 행의 레이아웃입니다. 이제 시작 레이아웃을 게시하겠습니다. – Javacadabra

답변

0

Each view of the list has a number picker associated with it

onCreate 메서드의 NumberPicker을 주 레이아웃에 정의해야합니다. 이후 NPE를 얻지 못했습니다.

NumberPicker으로 무엇을할지는 가장 적합한 곳에서 가져 오는 것이 좋습니다. 문제의 목록 항목보기에만 해당해야합니다.

+0

나는 당신이 말한 것과 똑같이 잘 수행했습니다. 고맙습니다! – Javacadabra

+0

나는 당신을 도왔다 니 기쁩니다! –

+0

각 목록 행에는 'NumberPicker'인스턴스가 있습니다. 하나의 인스턴스를 활동 필드에 넣는 것은 나에게 맞지 않습니다. – Henry

1

은을 가져올 수 : 위의 문이 지금 얼마나 나를 NumberPickers 컨트롤이 onCreate()에 대, 참조 및 getView() 방법에 MenuItemArrayAdapter에 초기화해야한다고 생각합니다

관련 문제