¿Cómo puedo abrir un fragmento con onItemClick
, o voy mal encaminado? Los activity sin problemas, pero los fragments me da error.
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.herprogramacion.restaurantericoparico.R;
import com.herprogramacion.restaurantericoparico.adapters.Calendario_Volley_Adapter;
import com.herprogramacion.restaurantericoparico.tabs.TabActual;
import com.herprogramacion.restaurantericoparico.ui.ActividadConfiguracion;
public class Frag_Calendario_Cadetes extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeLayout;
Fragment fragment;
// Atributos
ListView listView;
ArrayAdapter adapter;
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.jornadas_list, null);
TextView miTexto = (TextView)view.findViewById(R.id.mi_java);
miTexto.setText("CALENDARIO CADETES");
swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
// Obtener instancia de la lista
listView = (ListView) view.findViewById(android.R.id.list);
// Crear adaptador y setear
adapter = new Calendario_Volley_Adapter(getActivity());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position){
case 0:
fragment = new TabActual();
break;
case 2:
Intent intent = new Intent(getActivity(), ActividadConfiguracion.class);
startActivity(intent);
break;
}
if (fragment != null) {
fragmentManager
.beginTransaction()
.replace(R.id.swipe_container, fragment)
.commit();
}
}
private FragmentManager getSupportFragmentManager() {
return null;
}
});
return view;
}
@override
public void onRefresh() {
if (swipeLayout!=null) {
swipeLayout.setRefreshing(false);
swipeLayout.destroyDrawingCache();
swipeLayout.clearAnimation();
}
}
public boolean OnCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_cadetes, menu) ;
return false;
}
@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);
}
}
Esta es mi jornadas_list.xml que creo que es donde esta el error
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/mi_java"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ALGO FALLA"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#db1616"
android:gravity="center" />
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/calendario_row"
android:divider="@drawable/linea"
android:dividerHeight="5dp">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
el logcat:
FATAL EXCEPTION: main Process: com.rafelcf, PID: 23169 Theme: themes:{default=overlay:com.franzyroy.black.side, iconPack:com.franzyroy.black.side, fontPkg:com.franzyroy.black.side, com.android.systemui=overlay:com.franzyroy.black.side, com.android.systemui.navbar=overlay:system} java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference at com.herprogramacion.restaurantericoparico.fragments.Frag_Calendario_Cadetes$1.onItemClick(Frag_Calendario_Cadetes.java:77)