1

I got a problem with my OnClickListener and can't find my mistake! I tried to use fragments, the view they come from fragment_app_launch.xml I tried to use a Toast to test whether the OnClickListener is called, but it seems its not!

package com.pthuermer.juraquiz;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityAppLaunch extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    ActionBar actionbar = getSupportActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionbar.setTitle(R.string.app_name);

    ActionBar.Tab fragTab1 = actionbar.newTab().setText(R.string.tabBar_1);
    Fragment fragmentQuiz = new FragmentAppLaunch();
    fragTab1.setTabListener(new TabListener(fragmentQuiz));
    actionbar.addTab(fragTab1);

    ActionBar.Tab fragTab2 = actionbar.newTab().setText(R.string.tabBar_2);
    Fragment fragmentStatistics = new FragmentStatistics();
    fragTab2.setTabListener(new TabListener(fragmentStatistics));
    actionbar.addTab(fragTab2);

    ActionBar.Tab fragTab3 = actionbar.newTab().setText(R.string.tabBar_3);
    Fragment fragmentProVersion = new FragmentProVersion();
    fragTab3.setTabListener(new TabListener(fragmentProVersion));
    actionbar.addTab(fragTab3);

    ActionBar.Tab fragTab4 = actionbar.newTab().setText(R.string.tabBar_4);
    Fragment fragmentInfo = new FragmentInfo();
    fragTab4.setTabListener(new TabListener(fragmentInfo));
    actionbar.addTab(fragTab4);

    ActionBar.Tab fragTab5 = actionbar.newTab().setText(R.string.tabBar_5);
    Fragment fragmentSettings = new FragmentSettings();
    fragTab5.setTabListener(new TabListener(fragmentSettings));
    actionbar.addTab(fragTab5);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.app_launch, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    private Button btn_q_c_1;
    private Button btn_q_c_2;
    private Button btn_q_c_3;
    private Button btn_q_c_4;

    public PlaceholderFragment() {
    }

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

        View rootView = inflater.inflate(R.layout.fragment_app_launch, container, false);

        btn_q_c_1 = (Button)rootView.findViewById(R.id.btn_q_c_1);
        btn_q_c_1.setOnClickListener(btn_q_c_listener);
        btn_q_c_2 = (Button)rootView.findViewById(R.id.btn_q_c_2);
        btn_q_c_2.setOnClickListener(btn_q_c_listener);
        btn_q_c_3 = (Button)rootView.findViewById(R.id.btn_q_c_3);
        btn_q_c_3.setOnClickListener(btn_q_c_listener);
        btn_q_c_4 = (Button)rootView.findViewById(R.id.btn_q_c_4);
        btn_q_c_4.setOnClickListener(btn_q_c_listener);

        return rootView;

    }

    private OnClickListener btn_q_c_listener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getActivity(), "TESTTOAST", Toast.LENGTH_LONG).show();

            if (v == btn_q_c_1) {
                Globals.setQUESTION_CAT(1);
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
            }
            else if (v == btn_q_c_2) {
                Globals.setQUESTION_CAT(2);
                startActivity(new Intent(getActivity(), ActivityQuiz.class));

            }
            else if (v == btn_q_c_3) {
                Globals.setQUESTION_CAT(3);
                startActivity(new Intent(getActivity(), ActivityQuiz.class));

            }
            else if (v == btn_q_c_4) {
                Globals.setQUESTION_CAT(0);
                startActivity(new Intent(getActivity(), ActivityQuiz.class));
            }
            else {
                // Log
            }

        }

    };

}

class TabListener implements ActionBar.TabListener {

    private Fragment fragment;

    public TabListener(Fragment fragment) {
        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        // TODO Auto-generated method stub
        ft.replace(R.id.container, this.fragment);

    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

}

}

xml

<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:background="#edf8ff"
tools:context="com.pthuermer.juraquiz.AppLaunch$PlaceholderFragment" >

<TextView
    android:id="@+id/tv_heading_app_launch"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/heading_app_launch"
    android:textSize="20sp"
    android:gravity="center"
    android:background="#ffffff" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tv_heading_app_launch"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_q_c_1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="@string/btn_q_c_1" />

    <Button
        android:id="@+id/btn_q_c_2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="@string/btn_q_c_2" />

    <Button
        android:id="@+id/btn_q_c_3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="@string/btn_q_c_3" />

    <Button
        android:id="@+id/btn_q_c_4"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="@string/btn_q_c_4" />

</LinearLayout>

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Philip
  • 1,068
  • 3
  • 12
  • 21

2 Answers2

0

Try removing the static from your PlaceholderFragment.

Julisch
  • 308
  • 1
  • 16
  • 1
    Then try defining your `OnClickListener` within `onCreate()` before you assign it to the buttons. Maybe android is just putting a copy of the listener onto the button, but the listener that's copied doesn't have the `onClick()` method at that time, yet. – Julisch Mar 17 '14 at 22:10
  • you mean onClick="btw_q_c_listener" in XML? – Philip Mar 17 '14 at 22:15
0

Try the following code:

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

    View rootView = inflater.inflate(R.layout.fragment_app_launch, container, false);

    return rootView;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    OnClickListener btn_q_c_listener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        Toast.makeText(getActivity(), "TESTTOAST", Toast.LENGTH_LONG).show();

        if (v == btn_q_c_1) {
            Globals.setQUESTION_CAT(1);
            startActivity(new Intent(getActivity(), ActivityQuiz.class));
        }
        else if (v == btn_q_c_2) {
            Globals.setQUESTION_CAT(2);
            startActivity(new Intent(getActivity(), ActivityQuiz.class));

        }
        else if (v == btn_q_c_3) {
            Globals.setQUESTION_CAT(3);
            startActivity(new Intent(getActivity(), ActivityQuiz.class));

        }
        else if (v == btn_q_c_4) {
            Globals.setQUESTION_CAT(0);
            startActivity(new Intent(getActivity(), ActivityQuiz.class));
        }
        else {
            // Log
        }
    };

    btn_q_c_1 = (Button)getView().findViewById(R.id.btn_q_c_1);
    btn_q_c_1.setOnClickListener(btn_q_c_listener);
    btn_q_c_2 = (Button)getView().findViewById(R.id.btn_q_c_2);
    btn_q_c_2.setOnClickListener(btn_q_c_listener);
    btn_q_c_3 = (Button)getView().findViewById(R.id.btn_q_c_3);
    btn_q_c_3.setOnClickListener(btn_q_c_listener);
    btn_q_c_4 = (Button)getView().findViewById(R.id.btn_q_c_4);
    btn_q_c_4.setOnClickListener(btn_q_c_listener);
}
Julisch
  • 308
  • 1
  • 16
  • Then could you please try get that fragment out of `ActivityAppLaunch`? Go and put it into a separate java file named `PlaceholderFragment.java`, remove `static` and try again. **Hint:** You could try using only one instance of that Fragment by calling the constructor only once instead of calling it every time you want to display the fragment. Call the constructor one time and store its instance. And then, when you're about to display it, just take that instance and don't call the constructor again. – Julisch Mar 17 '14 at 23:22
  • right now I can't try nothing. Eclipse won't create R.java... I'm stuck – Philip Mar 17 '14 at 23:24
  • Have a look at [this](http://stackoverflow.com/questions/2757107/developing-for-android-in-eclipse-r-java-not-generating) – Julisch Mar 17 '14 at 23:26