2012-03-11 64 views
0

我製作了2個調整器,而我的第二個調整器依賴於第一個調整器。當我點擊第二個微調時,我的程序崩潰。這是我的logcat說什麼:spinner onItemSelected製作問題

03-11 12:09:35.076: ERROR/AndroidRuntime(367): FATAL EXCEPTION: main 
03-11 12:09:35.076: ERROR/AndroidRuntime(367): java.lang.NullPointerException 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.AbsListView.obtainView(AbsListView.java:1409) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.ListView.onMeasure(ListView.java:1127) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.view.View.measure(View.java:8313) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
03-11 12:09:35.076: ERROR/AndroidRuntime(367):  at android.view.View.measure(View.java:8313) 

這裏是我的代碼:

package com.test; 


import java.io.IOException; 

import org.json.JSONException; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class SearchActivity extends Activity implements AdapterView.OnItemSelectedListener 
{ 

    boolean flag=true; 
    Spinner spinner; 
    public String [] doctorsname=new String[1000]; 

    String[] items = {"psychologist", "neurologist","dentist"}; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setContentView(R.layout.searchact); 

     for(int i=0;i<1000;i++) 
     doctorsname[i]=null; 

     Button b=(Button)findViewById(R.id.button2); 
     Button b1=(Button)findViewById(R.id.button3); 

     spinner = (Spinner) findViewById(R.id.spinner1); 
     ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,items); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); 

     spinner.setOnItemSelectedListener(this); 

     b.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) 
     { 
      Intent i = new Intent(SearchActivity.this,DocprofileextActivity.class); 
      startActivity(i);  
     } 
    }); 

     b1.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) 
     { 
      Intent i = new Intent(SearchActivity.this,DocprofileextActivity.class); 
      startActivity(i);  
     } 
    }); 
    } 

    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) 
    { 

     if(parent==spinner) 
     { 
      Toast toast = Toast.makeText(this, items[position], 2000); 
      toast.show(); 

      logintosever obj1=new logintosever(); 

      try 
      { 
       doctorsname=obj1.searchbycategory(items[position]); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (JSONException e) 
      { 
       e.printStackTrace(); 
       flag=false; 
      } 

      Spinner spinner = (Spinner) findViewById(R.id.doctorsname); 

      if(doctorsname!=null) 
      { 
       ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,doctorsname); 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       spinner.setEnabled(true); 
       spinner.setAdapter(adapter); 
      } 
      else 
      { 
       spinner.setEnabled(false); 
      } 
     } 

    } 

    public void onNothingSelected(AdapterView<?> arg0) 
    { 

    } 


    public void onItemSelected2(AdapterView<?> parent, View view, int position,long id) 
    { 
    } 


} 
+0

你的命名規範是如此醜陋,,,,這兩個微調名稱是相同的,你把secon微調列表的位置? – 2012-03-11 07:38:55

+0

thnx爲你的迴應samir,好吧我會改變名字,而且我已經看到了教程中的人做了2個旋轉器,但有1個監聽器,並且他通過「this」來識別它,就像我爲spinner1做的一樣 – 2012-03-11 07:42:15

+0

您正在將空數組傳遞給數組適配器。請只發布您認爲可能發生錯誤的代碼。所以它會更具可讀性和可追蹤性。 – AAnkit 2012-03-11 08:04:02

回答

0

我首先把我的意見你,因爲他們似乎更重要:

  • 請到回到你的舊的問題和接受的答案 - how does accepting an answer works - 這也解釋了爲什麼你應該..
  • 請您下次發佈代碼的時間儘量去除不相關的合作它會堆積你的機會讓人們閱讀你的代碼。

關於你的問題,我真的不知道,但它的一個NullPointerException這意味着你要使用在一個地方一個空值的變量不接受空 也許試圖改變你的麪包變量這 -

Toast toast = Toast.makeText(this, "item:"+items[position], 2000); 

所以你不能把你的烤麪包內空..

0

主要問題是您的數組的大小是與你錯配適配器假設,你的陣列是10元的,你的適配器將只有5元從它,

裝置 串[10] = { 「一」, 「B」, 「C」, 「d」, 「e」 的};