2016-04-30 15 views
0

私は、fragment_tab.xmlでスピナーを作成しました。私はMainActivityの配列アダプターからスピナーを挿入しようとしましたが、それはヌルポインターの例外を与えています。 私の質問は、それを行う具体的な方法は何ですか?多くを検索した後、私は方法を見つけることができませんでした。ここで フラグメントレイアウトで作成されたスピナーをアクティビティに使用する方法

public class MainActivity extends AppCompatActivity { 

TabLayout tabLayout; 
Toolbar toolbar; 
ViewPager viewPager; 
ViewPagerAdapter viewPagerAdapter; 
Spinner spinner; 
List<String> places; 

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


public void tabLayoutConfigure() 
{ 
    toolbar = (Toolbar) findViewById(R.id.toolBar); 
    setSupportActionBar(toolbar); 
    viewPager = (ViewPager) findViewById(R.id.viewPager); 
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(viewPagerAdapter); 
    tabLayout = (TabLayout) findViewById(R.id.tabLayout); 
    tabLayout.setupWithViewPager(viewPager); 

} 

public void AddSpinner() 
{ 
    this.spinner= (Spinner)findViewById(R.id.spinner); 
    places= new ArrayList<String>(); 
    places.add("Regional office"); 
    places.add("Farm office"); 
    ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,places); 

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



    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      String item = parent.getItemAtPosition(position).toString(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
     }); 
    } 
} 

は私TabFragmentクラスです(フラグメント): はここに私のMainActivityある

public class TabFragment extends android.support.v4.app.Fragment { 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_tab,container,false); 
} 
} 

activity_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/mainCoordinatorLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appBarLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolBar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/textColorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/textColorPrimary" /> 

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

fragment_tab.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollView" > 

    <LinearLayout 
     android:orientation="vertical" 
     android:background="#689F38" 
     android:padding="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:text="Enrollment place" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner" /> 

任意の提案は大きな助けになります!

+0

更新:xmlファイルは –

答えて

3

私はあなたが達成しようとしていることについてはあまりよく分かりませんが、あなたは活動のスピナーを初期化しています。フラグメントレイアウトやアクティビティレイアウトで "R.id.spinner"を宣言してください。フラグメントレイアウトで宣言した場合、アクティビティはビューを見つけることができません。フラグメント内のスピナーコードを移動することができます。

ただし、あなたがアクティビティとフラグメントxmlを投稿できる場合は、簡単に把握できます。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view= inflater.inflate(R.layout.fragment_tab,container,false); 
    AddSpinner(view); 
    return view; 
} 

public void AddSpinner(View view) 
{ 
    spinner= (Spinner)view.findViewById(R.id.spinner2); 
    places= new ArrayList<String>(); 
    places.add("Regional office"); 
    places.add("Farm office"); 
    ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,places); 

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

をスピナー=(スピナー)view.findViewById(R.id.spinner) 問題を解決された使用:

+1

です。はい、私はあなたの断片にメソッドを移動する必要があります。 'populateSpinner(ArrayList items)'のようなものを作成し、メインクラスのフラグメントを参照して、その引数にその引数を渡します。 –

+0

@Praveen、はい、フラグメントレイアウトでスピナーを宣言しましたが、それは間違いです。私は活動のビューを見つけたい。助けてください。 –

+0

@SefatNoor私は、アクティビティでそれを初期化したい場合、フラグメント内のビューを宣言する目的を理解できませんが、アクティビティでスピナーの選択を聞きたい場合は、それを処理するコールバックを実装できます。私は正確にあなたが達成したいと思うものを知っています。 –

関連する問題