0

activity_table.xml에 ViewPager를 생성 중입니다.Android : ListView에 어댑터 추가시 NullPointerException

이 ViewPager는 동일한 레이아웃 (예 : list.xml)을 공유하는 두 개의 페이지가 있습니다.
이 레이아웃에는 어댑터를 만들려고 시도하는 ListView이 있습니다.

응용 프로그램은 제대로 컴파일하지만 충돌은 '무효 android.widget.ListView.setAdapter (android.widget을 가상 메소드를 호출하려고 할 때

NullPointerException이 함께 ListView에 상기 어댑터를 추가하려고 할 때. ListAdapter) '를 참조하십시오.

내가보기에 포함 된 레이아웃을 사용하려면 먼저 레이아웃을 "로드"해야한다고 들었지만 어떻게해야합니까?

내가 setContentView(R.layout.list)을 시도하지만,이 같은 오류가 발생합니다 ...

Table.java

SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    ViewPager viewPager = (ViewPager) findViewById(R.id.container); 
    viewPager.setAdapter(sectionsPagerAdapter); 

    String[] tmp = new String[mainTable.length-2]; 
    ListView listView = (ListView)findViewById(R.id.listView); 
    ListAdapter listAdapter = new ListAdapter(this,R.layout.list_item,tmp); 
    listView.setAdapter(listAdapter); 

public static class TableFragment extends Fragment { 
    private static final String ARG_SECTION_NUMBER = "section number"; 

    public TableFragment() { 
    } 
    public static TableFragment newInstance(int sectionNumber) { 
     TableFragment fragment = new TableFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER,sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.list,container,false); 
     TextView title = (TextView) rootView.findViewById(R.id.title); 

     if(getArguments().getInt(ARG_SECTION_NUMBER) == 1) 
      title.setText("Heute"); 
     else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2) 
      title.setText("Morgen"); 

     return rootView; 
    } 
} 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return TableFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     return 2; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "today"; 
      case 1: 
       return "tomorrow"; 
     } 
     return null; 
    } 
} 


public class ListAdapter extends ArrayAdapter<String> { 
    public ListAdapter(Context context, int textViewResourceId) { 
     super(context,textViewResourceId); 
    } 
    public ListAdapter(Context context, int resource, String[] items) { 
     super(context,resource,items); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 

     if(v == null) { 
      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.list_item, null); 
     } 

     String p = getItem(position); 

     if(p!=null) { 
      TextView grade = (TextView) v.findViewById(R.id.grade); 
      TextView hour = (TextView) v.findViewById(R.id.hour); 
      TextView course = (TextView) v.findViewById(R.id.course); 
      TextView teacher = (TextView) v.findViewById(R.id.teacher); 
      TextView room = (TextView) v.findViewById(R.id.room); 
      TextView description = (TextView) v.findViewById(R.id.description); 

      try { 
       grade.setText(tableData[position][1]); 
       hour.setText(tableData[position][2]); 
       course.setText(tableData[position][3]); 
       teacher.setText(tableData[position][4]); 
       room.setText(tableData[position][5]); 
       description.setText(tableData[position][6]); 
      } catch (ArrayIndexOutOfBoundsException e) { 
       return v; 
      } 
     } 

     return v; 
    } 
} 

activity_table.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="de.fuchstim.vertretungsplan.Table"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:layout_gravity="bottom|right" 
     app:srcCompat="@drawable/reload_arrow_white" /> 


</android.support.design.widget.CoordinatorLayout> 

list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:text="Heute (25.10.16)" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/title" 
    android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle" 
    android:textAlignment="center" /> 

<GridLayout android:layout_gravity="start" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/grade" 
     android:layout_height="wrap_content" 
     android:text="Klasse" 
     android:layout_row="0" 
     android:layout_column="0" 
     android:layout_width="0dp" 
     android:textSize="14sp" 
     android:layout_columnWeight="40" 
     android:paddingStart="10dp" /> 

    <TextView 
     android:text="Stunde" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/hour" 
     android:layout_row="0" 
     android:layout_column="1" 
     android:layout_columnWeight="15"/> 

    <TextView 
     android:text="Lehrer" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/teacher" 
     android:layout_row="0" 
     android:layout_column="3" 
     android:layout_columnWeight="15"/> 

    <TextView 
     android:text="Kurs" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/course" 
     android:layout_row="0" 
     android:layout_column="2" 
     android:layout_columnWeight="15"/> 

    <TextView 
     android:text="Raum" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/room" 
     android:layout_row="0" 
     android:layout_column="4" 
     android:layout_columnWeight="15"/> 

</GridLayout> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentStart="true" 
    android:id="@+id/listView" /> 

</LinearLayout> 
+1

setContentView에는 레이아웃이 필요합니다. 약간의 코드보기 – Xjasz

+1

관련 코드를 게시하십시오. 나는 다른 바지에 크리스탈 볼을두고 갔다. – Robert

+0

Ive가 관련 코드에 대한 링크를 추가했습니다 –

답변

0

내가 된 setContentView (R.layout.list)를 시도하지만,이 같은 오류가 발생합니다 ...

첫째, 당신은 대신 setContentView(R.layout.activity_table)를 사용해야합니다.

둘째, ListView가 Fragment 레이아웃에 포함되어 있으므로 Activity에서 ListView를 찾을 수 없습니다.

여기를 참조하십시오? ... 귀하의 ListView는이 제목 요소와 동일한 레이아웃에, 그래서 당신은 여기에있는 ListView를 찾을 필요가

View rootView = inflater.inflate(R.layout.list,container,false); 
TextView title = (TextView) rootView.findViewById(R.id.title); 

이동 그 조각에 활동에서의 ListView 코드. Context를 얻으려면 this 대신 Fragment 내의 Adapter에 getActivity()을 사용하십시오.

여기가 @+id/listView 값입니다. 해당 레이아웃은 rootView.findViewById으로 검색됩니다.

+0

그게 작동합니다.^^ –

+0

또 하나의 질문입니다; ListAdapter에 의해 TextViews의 내용이 변경되도록하려면 어떻게 변경해야합니까? doesnt는 atm에서 작동하는 것처럼 보입니다 ... –

+0

'tableData'의 값이 무엇인지 표시하지 않았기 때문에 나는 무엇을 의미하는지 모르겠습니다. 다른 질문이 있으면 [Ask Question] (// stackoverflow.com/questions/ask) 버튼을 클릭하여 질문하십시오. –

0

이 위치에서 널 포인터를하지 않았다, 이후 : viewpager은 위의 레이아웃에 있기 때문에

setContentView(R.layout.activity_table); 

및 목록보기이기 때문에 : 그래서

ViewPager viewPager = (ViewPager) findViewById(R.id.container); 
viewPager.setAdapter(sectionsPagerAdapter); 

, 이것은 당신이 사용하는 의미 이 레이아웃 XML 파일이 아니라면, 널 포인터를 얻게됩니다 :

ListView listView = (ListView)findViewById(R.id.listView); 
ListAdapter listAdapter = new ListAdapter(this,R.layout.list_item,tmp); 
listView.setAdapter(listAdapter); 

null, activity_table xml 파일에 없습니다.

당신은 OnCreateView()에 어댑터를 설정해야이 도움이

@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.list,container,false); 
    TextView title = (TextView) rootView.findViewById(R.id.title); 

    // Add below ... 
    String[] tmp = new String[mainTable.length-2]; 
    ListView listView = (ListView)rootView.findViewById(R.id.listView); 
    ListAdapter listAdapter = new ListAdapter(getActivity(),R.layout.list_item,tmp); 
    listView.setAdapter(listAdapter); 

    // ends here ... 

    if(getArguments().getInt(ARG_SECTION_NUMBER) == 1) 
     title.setText("Heute"); 
    else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2) 
     title.setText("Morgen"); 

    return rootView; 
} 

희망을!

+0

'새 ListAdapter (this는'새 ListAdapter (getActivity()입니다. –

+0

예, 수정했습니다. 감사합니다. – DiName

+0

대단히 감사합니다.^ –

관련 문제