我的OnClickListener出现问题,无法找到错误! 我试图使用片段,它们来自fragment_app_launch.xml 我尝试使用Toast来测试是否调用了OnClickListener,但它似乎不是!
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>
答案 0 :(得分:0)
尝试从static
删除PlaceholderFragment
。
答案 1 :(得分:0)
请尝试以下代码:
@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);
}