Hallo, ich habe ein Problem bei der Kommunikation zwischen den Fragmenten.
Beim Drücken eines Buttons im ersten Tab soll im zweiten Tab sich ein Text verändern aber das bekomme ich leider nicht hin. Der Compiler gibt mir den NullPointerException aus. Ich habe viel recherchiert aber nichts hilfreiches gefunden.
Ich bin noch ein Anfänger und würde mich freuen, wenn mir jemand helfen kann
Code
package info.androidhive.tabsswipe;
public interface ActivityToMovies { public void activityConnection(boolean merkerMovies);}
Code
package info.androidhive.tabsswipe;
public interface GamesToActivity { public void connectionToActivity(boolean merker);
}
Code
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;import android.annotation.SuppressLint;import android.app.ActionBar;import android.app.ActionBar.Tab;import android.app.FragmentTransaction;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;
@SuppressLint("NewApi")public class MainActivity extends FragmentActivity implements ActionBar.TabListener,GamesToActivity { ActivityToMovies connection; public boolean merkerActivity; private ViewPager viewPager; private TabsPagerAdapter mAdapter; private ActionBar actionBar; // Tab titles private String[] tabs = { "Top Rated", "Games", "Movies" }; private String data = "Hallo es geht";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initilization viewPager = (ViewPager) findViewById(R.id.pager); actionBar = getActionBar(); mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter); actionBar.setHomeButtonEnabled(false); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs for (String tab_name : tabs) { actionBar.addTab(actionBar.newTab().setText(tab_name) .setTabListener(this)); }
/** * on swiping the viewpager make respective tab selected * */ viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override public void onPageSelected(int position) { // on changing the page // make respected tab selected actionBar.setSelectedNavigationItem(position); }
@Override public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override public void onPageScrollStateChanged(int arg0) { } }); }
@Override public void onTabReselected(Tab tab, FragmentTransaction ft) { }
@Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // on tab selected // show respected fragment view viewPager.setCurrentItem(tab.getPosition()); }
@Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { }
@Override public void connectionToActivity(boolean merker) { // TODO Auto-generated method stub merker = this.merkerActivity; connection.activityConnection(merker); }
}
Alles anzeigen
Code
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;
public class GamesFragment extends Fragment implements OnClickListener{ public Button btn; GamesToActivity connection; public boolean merker = false;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false); btn = (Button)rootView.findViewById(R.id.button1); btn.setOnClickListener(this); return rootView; }
@Override public void onClick(View arg0) { // TODO Auto-generated method stub if(arg0.getId() == btn.getId()){ //set TEXT merker = true; connection.connectionToActivity(merker); } }
}
Alles anzeigen
Code
package info.androidhive.tabsswipe;
import info.androidhive.tabsswipe.R;import android.R.bool;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;
public class MoviesFragment extends Fragment implements ActivityToMovies { TextView txt; public boolean merker = false;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false); txt = (TextView)rootView.findViewById(R.id.textView1); if(merker == true){ txt.setText("Die Verbindung erfolgreich"); } else{ txt.setText("Keine Verbindung"); } return rootView; }
@Override public void activityConnection(boolean merkerMovies) { // TODO Auto-generated method stub this.merker = merkerMovies; }
}
Alles anzeigen