我有这个活动:
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class ImagenesAlumnoActivity 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_imagenes_alumno);
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);
}
public int getScreenWidth() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
return width;
}
@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_imagenes_alumno, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.ajustes:
return true;
case R.id.modo_edicion:
Intent intent = new Intent(ImagenesAlumnoActivity.this, ModoAlumnoActivity.class);
intent.putExtra("alumno", "Alumno");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private GridViewImageAdapter adapter1;
private GridViewImageAdapter adapter2;
public GridView gridView;
public GridView gridSiNo;
private int anchoColumna;
private List<Imagen> imagenes = new ArrayList<Imagen>();
private List<Imagen> siNo = new ArrayList<Imagen>();
MediaPlayer player;
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
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.fragment_imagenes_alumno, container, false);
ArrayList<Imagen> imagenesParaMostrar = new ArrayList<Imagen>();
gridView = (GridView) rootView.findViewById(R.id.grid_view);
gridSiNo = (GridView) rootView.findViewById(R.id.gridSiNo);
double mult = 0.8;
double mult2 = 0.2;
inicializarGrilla(Constantes.CANTIDAD_COLUMNAS, Constantes.PADDING_GRILLA, gridView, mult);
inicializarGrilla((Constantes.CANTIDAD_COLUMNAS - 2), Constantes.PADDING_GRILLA, gridSiNo, mult2);
int position = this.getArguments().getInt(ARG_SECTION_NUMBER);
imagenesParaMostrar.clear();
siNo.clear();
for (Imagen imagen : Datos.imagenes){
if ((imagen.getCategoria()) == position ) {
imagenesParaMostrar.add(imagen);
}
}
siNo.add(new Imagen("Si", "si"));
siNo.add(new Imagen("No", "no"));
adapter1 = new GridViewImageAdapter(this.getActivity(),imagenesParaMostrar,anchoColumna);
gridView.setAdapter(adapter1);
adapter2 = new GridViewImageAdapter(this.getActivity(), siNo, anchoColumna);
gridSiNo.setAdapter(adapter2);
return rootView;
}
private void inicializarGrilla(int cantidadColumnas, int paddingGrilla, GridView gv, double mult) {
anchoColumna = (int) (((this.getScreenWidth()* mult) - ((cantidadColumnas + 1) * paddingGrilla)) /cantidadColumnas);
gv.setNumColumns(cantidadColumnas);
gv.setColumnWidth(anchoColumna);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setPadding(paddingGrilla, paddingGrilla, paddingGrilla, paddingGrilla);
gv.setHorizontalSpacing(paddingGrilla);
gv.setVerticalSpacing(paddingGrilla);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Imagen imagen = (Imagen) gridView.getAdapter().getItem(position);
player = MediaPlayer.create(getActivity(), imagen.getSonido());
player.start();
}
});
}
public int getScreenWidth() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
return width;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Bundle b = getIntent().getExtras();
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).
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
// Show 4 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "PISTA";
case 1:
return "ESTABLO";
case 2:
return "EMOCIONES";
case 3:
return "NECESIDADES";
case 4:
return b.getString("alumno");
}
return null;
}
}
public void loadBitmap(int resId, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView, getResources());
task.execute(resId);
}
}
我有一个动作栏,在这一个我有两个选择。当我选择Modo Edicion时,我想去另一个活动。
以下是其他活动的代码:
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Lupi on 27/12/2015.
*/
public class ModoAlumnoActivity extends AppCompatActivity{
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_modo_edicion);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
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.container1);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs1);
tabLayout.setupWithViewPager(mViewPager);
}
@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_modo_edicion, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.ajustes:
return true;
case R.id.modo_edicion:
Intent intent = new Intent(ModoAlumnoActivity.this, ImagenesAlumnoActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private GridViewImageAdapter adapter1;
private GridViewImageAdapter adapter2;
public GridView gridView;
public GridView gridSiNo;
private int anchoColumna;
private List<Imagen> imagenes = new ArrayList<Imagen>();
private List<Imagen> siNo = new ArrayList<Imagen>();
MediaPlayer player;
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
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.fragment_imagenes_alumno, container, false);
ArrayList<Imagen> imagenesParaMostrar = new ArrayList<Imagen>();
gridView = (GridView) rootView.findViewById(R.id.grid_view);
gridSiNo = (GridView) rootView.findViewById(R.id.gridSiNo);
double mult = 0.8;
double mult2 = 0.2;
inicializarGrilla(Constantes.CANTIDAD_COLUMNAS, Constantes.PADDING_GRILLA, gridView, mult);
inicializarGrilla((Constantes.CANTIDAD_COLUMNAS - 2), Constantes.PADDING_GRILLA, gridSiNo, mult2);
int position = this.getArguments().getInt(ARG_SECTION_NUMBER);
imagenesParaMostrar.clear();
siNo.clear();
for (Imagen imagen : Datos.imagenes){ /* Hay que traer las imagenes del nene/a */
if ((imagen.getCategoria()) == position ) {
imagenesParaMostrar.add(imagen);
}
}
siNo.add(new Imagen("Si", "si"));
siNo.add(new Imagen("No", "no"));
adapter1 = new GridViewImageAdapter(this.getActivity(),imagenesParaMostrar,anchoColumna);
gridView.setAdapter(adapter1);
adapter2 = new GridViewImageAdapter(this.getActivity(), siNo, anchoColumna);
gridSiNo.setAdapter(adapter2);
return rootView;
}
private void inicializarGrilla(int cantidadColumnas, int paddingGrilla, GridView gv, double mult) {
anchoColumna = (int) (((this.getScreenWidth()* mult) - ((cantidadColumnas + 1) * paddingGrilla)) /cantidadColumnas);
gv.setNumColumns(cantidadColumnas);
gv.setColumnWidth(anchoColumna);
gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gv.setPadding(paddingGrilla, paddingGrilla, paddingGrilla, paddingGrilla);
gv.setHorizontalSpacing(paddingGrilla);
gv.setVerticalSpacing(paddingGrilla);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Imagen imagen = (Imagen) gridView.getAdapter().getItem(position);
player = MediaPlayer.create(getActivity(), imagen.getSonido());
player.start();
}
});
}
public int getScreenWidth() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
return width;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
Bundle b = getIntent().getExtras();
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).
return PlaceholderFragment.newInstance(position);
}
@Override
public int getCount() {
// Show 4 total pages.
return 1;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return b.getString("alumno");
}
return null;
}
}
public void loadBitmap(int resId, ImageView imageView) {
BitmapWorkerTask task = new BitmapWorkerTask(imageView, getResources());
task.execute(resId);
}
}
当我点击Modo Edicion时,应用会自行关闭,并返回此错误:
12-28 13:00:45.942 1337-1360 /? W / Binder:
捕获了绑定器存根实现中的RuntimeException。 Java.lang.NullPointerException
在android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
在com.android.internal.view.IInputMethod $ Stub.onTransact(IInputMethod.java:129)
在android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)12-28 13:00:45.942 1253年至1474年/? W / InputMethodManagerService: 得到RemoteException向pid 1791 uid 10056发送setActive(false)通知
有谁能告诉我如何解决它?感谢。