2013-08-25 98 views
0

嗨,我創建的應用程序,將從旋轉器中選擇數據(在列表視圖中),並希望選擇值。所以這是我的日誌貓信息從列表視圖中獲取微調器中的選定值?

08-25 09:13:28.786: W/dalvikvm(580): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
08-25 09:13:28.892: E/AndroidRuntime(580): FATAL EXCEPTION: main 
08-25 09:13:28.892: E/AndroidRuntime(580): java.lang.NullPointerException 
08-25 09:13:28.892: E/AndroidRuntime(580):  at com.example.dcsd.Result$1.onClick(Result.java:44) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.view.View.performClick(View.java:2485) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.view.View$PerformClick.run(View.java:9080) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.os.Handler.handleCallback(Handler.java:587) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.os.Looper.loop(Looper.java:123) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-25 09:13:28.892: E/AndroidRuntime(580):  at dalvik.system.NativeStart.main(Native Method) 

,這是我Result.java類

package com.example.dcsd; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class Result extends ListActivity{ 
Spinner sp; 
SpinnerWrapper wrapper=new SpinnerWrapper(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.result); 
    MyAdapter adap=new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules)); 
    setListAdapter(adap); 
    Button b=(Button) findViewById(R.id.button12); 
    final Spinner sp=(Spinner) findViewById(R.id.spinner1); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent intent=new Intent(Result.this,Cal.class); 
      for(int i=0;i<15;i++){ 
       //String text= sp.getSelectedItem().toString(); 
       //Log.d("This", "huu"); 
      } 
      sp.setOnItemSelectedListener(wrapper); 
      //intent.putExtra("values", sp.getSelectedItem().toString()); 
      //intent.putExtra("values", sp.setOnItemSelectedListener(wrapper)); 
      startActivity(intent);    
     } 
    }); 
} 

private class MyAdapter extends ArrayAdapter<String>{ 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
      String[] strings) { 
     super(context, resource, textViewResourceId, strings); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row=inflater.inflate(R.layout.item_layout, parent,false); 
     String[] items=getResources().getStringArray(R.array.modules); 
     TextView tv=(TextView) row.findViewById(R.id.textView1);   
     tv.setText(items[position]); 
     sp=(Spinner)row.findViewById(R.id.spinner1); 
     ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(Result.this, R.array.grades, android.R.layout.simple_spinner_dropdown_item); 
     sp.setAdapter(adapter); 
     //sp.setOnItemSelectedListener(wrapper); 
     //int dd=row.getId(); 
     return row;  
    } 
} 
} 

而且這樣

package com.example.dcsd; 

import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class SpinnerWrapper implements OnItemSelectedListener { 

public SpinnerWrapper() { 

} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) { 
    String tex=parent.getItemAtPosition(position).toString(); 
    Log.d("Selected", tex); 

} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

} 

我SpinnerWrapper.java類lokks我怎樣才能reslolve這問題??

+0

對不起,我我以前不得到你..其實你需要從微調獲得所選擇的項目,而你的onclick按鈕..我是正確... – Hariharan

+0

呀。這就是我需要的。但插入此語句後「sp.setOnItemSelectedListener(wrapper);」它會給出上述錯誤.. :) –

+0

@KMDilshanUdara你需要在'onCreate'上設置微調器上的適配器。將下面的內容移動到'onCreate'' ArrayAdapter adapter = ArrayAdapter.createFromResource(Result.this,R.array .grades,android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter)'; – Raghunandan

回答

0

試試這個..

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    ArrayList<String> spinner_items; 

    Spinner spinner_from; 
     Button btn;  


    ArrayAdapter<String> from_adapter; 


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

     spinner_items = new ArrayList<String>(); 
     spinner_items.add("Select"); 
     spinner_items.add("One"); 
     spinner_items.add("Two"); 
     spinner_items.add("Three"); 
     spinner_items.add("Four");  

     spinner_from = (Spinner) findViewById(R.id.spinner_from); 
       btn = (Button) findViewById(R.id.btn); 


     from_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, spinner_items); 
     from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner_from.setAdapter(from_adapter); 

btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

    Toast.makeText(getBaseContext(), ""+spinner_from.getSelectedItem().toString().trim(), Toast.LENGTH_SHORT).show(); 

      } 

     } 
    }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
+0

事情是在我的列表視圖我使用textview和spinner.so我使用由ArrayList類延伸的自定義適配器類和使用overide方法getView()。 –

相關問題