2014-09-03 1 views
0

이것은 오류가있는 곳의 코드입니다. 이 내 XML 코드Nullpointer setadapter spinner

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:text="@string/materias" 
     android:textSize="20sp" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:ems="10" 
     android:inputType="text" > 

    </EditText> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" > 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/creditos" 
     android:textSize="20sp" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:inputType="number" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" 
    android:layout_marginBottom="30sp"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Cantidad de Examenes" 
     android:textSize="20sp" /> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:textSize="20sp" 
       android:text="Examen" /> 
      <TextView 
       android:id="@+id/textView5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center" 
       android:textSize="20sp" 
       android:text="Nota" /> 
      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="20sp" 
       android:gravity="center" 
       android:layout_weight="2" 
       android:text="%" /> 

     </TableRow> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText6" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number"/> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText7" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText8" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText9" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number"/> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText10" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText11" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText12" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number"/> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText13" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText14" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText15" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number"/> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText16" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText17" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText18" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number"/> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow7" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:id="@+id/editText19" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="text"/> 

      <EditText 
       android:id="@+id/editText20" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="numberDecimal"/> 

      <EditText 
       android:id="@+id/editText21" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:ems="10" 
       android:inputType="number" /> 

     </TableRow> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/guardar" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:onClick="agregarNotas" 
     android:text="@string/materias" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:text="@string/nueva" /> 
</LinearLayout> 

public class MainActivity extends Activity implements OnItemSelectedListener{ 
private EditText promAcum; 
private EditText credCur; 
private EditText sem; 
private EditText num_examenes; 
private EditText editText13; 
private EditText editText14; 
private EditText editText15; 
private EditText editText16; 
private EditText editText17; 
private EditText editText18; 
private EditText editText19; 
private EditText editText20; 
private EditText editText21; 
private Spinner spinner1; 
public String valorPromAcum; 
public String valorCredCur; 
public String valorSem; 
public String[] numeroExamenes={"3","4","5","6"}; 
private final static String TAG_FRAGMENT = "TAG_FRAGMENT"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    FragmentManager fm = getFragmentManager(); 
    Fragment fragment = fm.findFragmentById(R.id.fragment_content); 

    if (fragment == null) { 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.add(R.id.fragment_content, new MainFragment()); 
     ft.commit(); 
    } 

    promAcum = (EditText) findViewById(R.id.promAcum); 
    credCur = (EditText) findViewById(R.id.credCur); 
    sem = (EditText) findViewById(R.id.sem); 
    editText13 = (EditText)findViewById(R.id.editText13); 
    editText14 = (EditText)findViewById(R.id.editText14); 
    editText15 = (EditText)findViewById(R.id.editText15); 
    editText16 = (EditText)findViewById(R.id.editText16); 
    editText17 = (EditText)findViewById(R.id.editText17); 
    editText18 = (EditText)findViewById(R.id.editText18); 
    editText19 = (EditText)findViewById(R.id.editText19); 
    editText20 = (EditText)findViewById(R.id.editText20); 
    editText21 = (EditText)findViewById(R.id.editText21); 

    spinner1 = (Spinner) findViewById(R.id.spinner1); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,numeroExamenes); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner1.setAdapter(dataAdapter); 

    addListenerOnSpinnerItemSelection(); 
} 

public void addListenerOnSpinnerItemSelection(){ 

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 

       int position, long id) { 
       Toast.makeText(MainActivity.this, 
         "On Button Click : " + 
         "\n" + String.valueOf(spinner1.getSelectedItem()) , 
         Toast.LENGTH_LONG).show(); 
      } 
     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      // TODO Auto-generated method stub 
     } 
    });} 

입니다

로그 캣 오류 라인 (78) : 내가 그 모든 그것의 확인을 생각하지만 난 오류 이 어디 잘 모릅니다은 내 코드입니다 spinner1 .setadapter (어댑터);

enter code here09-03 11:25:46.344: E/AndroidRuntime(7841): Caused by: java.lang.NullPointerException 
09-03 11:25:46.344: E/AndroidRuntime(7841): at com.jonathanar.promapp.MainActivity.onCreate(MainActivity.java:78) 

제안 당신은 Activity에서 뷰를 검색하려는

+0

은 xml 이름'activity_main'입니다. 그렇지 않은 경우 뷰는 아마 Fragment 레이아웃에 속합니다. – Raghunandan

답변

0

을 감사합니다,하지만 그들은 Fragment에 있습니다.

getActivity().findViewById(R.id.yourId);findViewById() 방법을 사용할 때마다해야 할 일입니다.

0

NullPointerException은 어댑터 인스턴스화에 문제가 아니라 Spinner를 얻는 방법과 관련이 있습니다.

조각에 회 전자가 있으면 컨텍스트를 지정해야합니다.

findViewById()를 그냥 두는 것은 this.findViewById()입니다. 여기서 this는 사용자의 작업입니다. 즉, 활동 내용보기에서보기를 찾는 방법을 알려줍니다.

스피너는 조각의 콘텐츠 뷰에있는 경우

, 당신은 대신 조각의 맥락에서 스피너를 찾을

fragment.getActivity().findViewById(R.id.???); 

이 방법 findViewById를() 알고를 사용해야합니다.

희망이 도움이됩니다.