-3

私はアンドロイドで新しくプロジェクトを作りたいですが、クリックしたときに別のアクティビティに渡すことができるタブ付きアクティビティの断片の中にリストビューを挿入する際に問題があります。私がここで尋ねる前に、私はすでに解決策を探そうとしましたが、うまくいかなかったので、ここにいる誰もが私の問題を解決できることを願っています。タブ付きアクティビティのフラグメント内のSetonitemclicklistener

ここに私のコード

サービス活動がTabホストとしてです

public class ServiceActivity extends AppCompatActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link FragmentPagerAdapter} derivative, which will keep every 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).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.menu_, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

//deleted PlaceHolderFragment 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     switch (position) { 
      case 0: 
       Sinspection sins = new Sinspection(); 
       return sins; 
      case 1: 
       Minspection mins = new Minspection(); 
       return mins; 
      case 2: 
       Linspection lins = new Linspection(); 
       return lins; 
      case 3: 
       WRinspection wrins = new WRinspection(); 
       return wrins; 
      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     // Show total pages. 
     return 4; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "S Inspection"; 
      case 1: 
       return "M Inspection"; 
      case 2: 
       return "L Inspection"; 
      case 3: 
       return "When Required"; 
     } 
     return null; 
    } 
} 

断片クラス

public class Linspection extends ListFragment { 

String [] linsp = { 
     "Differential Oil and Filter", 
     "Hub and Reduction Gear Oil", 
     "Wheel Nut", 
     "Air Dryer Filter" 
}; 
ArrayAdapter<String> adapter; 

ListView listView; 

@Override 
public View onCreateView(final LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    listView = (ListView)getActivity().findViewById(R.id.listViewL); 
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout. 
      simple_list_item_1,linsp); 
    setListAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      switch (position){ 
       case 0: 
        Intent intent = new Intent(getActivity(), EngineOil.class); 
        startActivity(intent); 
      } 
     } 
    }); 
    return super.onCreateView(inflater, container, savedInstanceState); 
} 

私はこれが私です

public class EngineOil extends AppCompatActivity { 

ViewPager viewPager; 


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    viewPager = (ViewPager) findViewById(R.id.viewPager); 

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this); 
    viewPager.setAdapter(viewPagerAdapter); 
} 
} 

この活動に渡したいですxml:

ACTIVITY_SERVICE

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
> 


<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listViewL" 
    > 
</ListView> 
</RelativeLayout> 

活動destionationのXML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:theme="?attr/actionBarTheme" 
    android:minHeight="?attr/actionBarSize" 
    android:id="@+id/toolbar3" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:id="@+id/viewPager" 
    android:layout_marginTop="20dp" 
    android:layout_below="@+id/toolbar3" 
    android:layout_alignParentStart="true"> 

</android.support.v4.view.ViewPager> 
</RelativeLayout> 

私はそれが "常にクラッシュし続ける" アプリケーションを実行します。私は

おかげで事前

+0

スタックトレースを見て、クラッシュの原因を特定します。 https://stackoverflow.com/questions/23353173 –

+0

クラッシュにはどんなエラーがありますか?あなたはlogcatを投稿できますか? – sam

答えて

0

に私はあなたがリファレンスとして使用するためのプロジェクトを行って、それを修正する方法を知っている、とあなたは私の問題を解決するために私を助けることができると思いません。

`activity_main` 
<?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"> 


<TabHost 
    android:id="@+id/tabHost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentBottom="true"> 

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:id="@+id/ll_cashier_invoice" 
        android:baselineAligned="false" 
        android:gravity="center_vertical" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:padding="4dip"> 

        <fragment 
         android:id="@+id/frag_invoice" 
         android:name="com.example.sydney.sample.MainActivity$FirstFragment" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="1" /> 
       </LinearLayout> 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab3" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/tab4" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

      </LinearLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 
</RelativeLayout> 

`fragment1.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"> 
    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/myArray" /> 

</LinearLayout> 

`MainActivity.java` 

public class MainActivity extends Activity { 

ListView lv; 
TabHost host; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    host = (TabHost)findViewById(R.id.tabHost); 
    host.setup(); 
    lv = (ListView)findViewById(R.id.list); 


    TabHost.TabSpec spec = host.newTabSpec("Sinspection"); 

    spec.setContent(R.id.tab1); 

    spec.setIndicator("Sinspection"); 

    host.addTab(spec); 

    //Tab 2 

    spec = host.newTabSpec("Minspection"); 

    spec.setContent(R.id.tab2); 

    spec.setIndicator("Minspection"); 

    host.addTab(spec); 

    //Tab 3 

    spec = host.newTabSpec("Linspection"); 

    spec.setContent(R.id.tab3); 

    spec.setIndicator("Linspection"); 

    host.addTab(spec); 

    //Tab 4 

    spec = host.newTabSpec("Winspection"); 

    spec.setContent(R.id.tab4); 

    spec.setIndicator("Winspection"); 

    host.addTab(spec); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent myIntent = new Intent(MainActivity.this,NextActivity.class); 
      startActivity(myIntent); 
     } 
    }); 
} 

public static class FirstFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment1, container, false); 
    } 
} 
} 

フルコードはhereをクリックしてください。

関連する問題