我正在为计算器编写代码,分别具有两个fragmenOne和fragmentTwo片段。 FragmentOne是我的默认片段,它包含基本功能,例如加,减和数字。第二个片段fragmentTwo包含高级功能,例如sin tan cos ...等。第二个片段中的按钮不起作用,并且不附加Editbox。但是我fragmentOne中的按钮可以正常工作。如果我转到fragmentTwo frist,然后转到fragmentOne,则按钮在fragmentOne中也不起作用。请关注此特定问题。下面提供了代码和图像。





MainActivity.java

package com.example.fragmentstest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;

public class MainActivity extends Activity implements View.OnClickListener {
Button one, two, three, four, five, six, seven, eight, nine, zero, add,
sub, mul, div, equal, decimal, cancel, sin, cos, tan, open_bracket, close_bracket;
EditText disp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    one = (Button) findViewById(R.id.one);
    two = (Button) findViewById(R.id.two);
    three = (Button) findViewById(R.id.three);
    four = (Button) findViewById(R.id.four);
    five = (Button) findViewById(R.id.five);
    six = (Button) findViewById(R.id.six);
    seven = (Button) findViewById(R.id.seven);
    eight = (Button) findViewById(R.id.eight);
    nine = (Button) findViewById(R.id.nine);
    zero = (Button) findViewById(R.id.zero);

    add = (Button) findViewById(R.id.add);
    sub = (Button) findViewById(R.id.sub);
    mul = (Button) findViewById(R.id.mul);
    div = (Button) findViewById(R.id.div);
    equal = (Button) findViewById(R.id.equal);
    decimal = (Button) findViewById(R.id.decimal);

    sin = (Button) findViewById(R.id.sin);
    cos = (Button) findViewById(R.id.cos);
    tan = (Button) findViewById(R.id.tan);
    open_bracket = (Button) findViewById(R.id.open_bracket);
    close_bracket = (Button)findViewById(R.id.close_bracket);

    disp = (EditText) findViewById(R.id.display_panel);
    cancel = (Button) findViewById(R.id.cancel);
    try {
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);

        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        mul.setOnClickListener(this);
        div.setOnClickListener(this);
        equal.setOnClickListener(this);
        decimal.setOnClickListener(this);
        cancel.setOnClickListener(this);
        sin.setOnClickListener(this);
        cos.setOnClickListener(this);
        tan.setOnClickListener(this);
        open_bracket.setOnClickListener(this);
        close_bracket.setOnClickListener(this);
    } catch (Exception e) {
    }
}

public void selectFrag(View view) {
    Fragment fr;

    if (view == findViewById(R.id.button2)) {
        fr = new FragmentTwo();

    } else {
        fr = new FragmentOne();

    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();

}

@Override
public void onClick(View v) {
    Editable str = disp.getText();
    switch (v.getId()) {
    case R.id.zero:
        str = str.append(zero.getText());
        disp.setText(str);
        break;
    case R.id.one:
        str = str.append(one.getText());
        disp.setText(str);
        break;
    case R.id.two:
        str = str.append(two.getText());
        disp.setText(str);
        break;
    case R.id.three:
        str = str.append(three.getText());
        disp.setText(str);
        break;
    case R.id.four:
        str = str.append(four.getText());
        disp.setText(str);
        break;
    case R.id.five:
        str = str.append(five.getText());
        disp.setText(str);
        break;
    case R.id.six:
        str = str.append(six.getText());
        disp.setText(str);
        break;
    case R.id.seven:
        str = str.append(seven.getText());
        disp.setText(str);
        break;
    case R.id.eight:
        str = str.append(eight.getText());
        disp.setText(str);
        break;
    case R.id.nine:
        str = str.append(nine.getText());
        disp.setText(str);
        break;
    case R.id.add:
        str = str.append(add.getText());
        disp.setText(str);
        break;
    case R.id.sub:
        str = str.append(sub.getText());
        disp.setText(str);
        break;
    case R.id.mul:
        str = str.append(mul.getText());
        disp.setText(str);
        break;
    case R.id.div:
        str = str.append(div.getText());
        disp.setText(str);
        break;
    case R.id.decimal:
        str = str.append(decimal.getText());
        disp.setText(str);
        break;
    case R.id.sin:
        str = str.append(sin.getText());
        disp.setText(str);
        break;
    case R.id.cos:
        str = str.append(cos.getText());
        disp.setText(str);
        break;
    case R.id.tan:
        str = str.append(tan.getText());
        disp.setText(str);
        break;
    case R.id.open_bracket:
        str = str.append(open_bracket.getText());
        disp.setText(str);
        break;
    case R.id.close_bracket:
        str = str.append(close_bracket.getText());
        disp.setText(str);
        break;
    case R.id.cancel:
        disp.setText("");
        disp.setHint("Perform Operation");
        break;
    case R.id.equal:
        String val;
        val = disp.getText().toString();
        Calculable calc = null;
        try {
            Calculable result = new ExpressionBuilder(val).build();
            disp.setText(Double.toString(result.calculate()));
            /* size_num = result.calculate(); */
        } catch (UnknownFunctionException e) {
            disp.setText("ERROR");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnparsableExpressionException e) {
            disp.setText("ERROR");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;

}

}
}


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
   android:id="@+id/display_panel"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="@string/dispHint"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:paddingRight="45dp"
    android:text="@string/basic" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="selectFrag"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:text="@string/adv" />

</LinearLayout>
<fragment
    android:id="@+id/fragment_place"
    android:name="com.example.fragmentstest.FragmentOne"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>


fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >

 <Button
    android:id="@+id/sin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sin" />

 <Button
     android:id="@+id/cos"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_toRightOf="@+id/sin"
     android:text="@string/cos" />

 <Button
     android:id="@+id/tan"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_toRightOf="@+id/cos"
     android:text="@string/tan" />

 <Button
     android:id="@+id/log"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/In"
     android:layout_alignBottom="@+id/In"
     android:layout_toRightOf="@+id/In"
     android:text="@string/log" />

 <Button
     android:id="@+id/not"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/log"
     android:layout_alignBottom="@+id/log"
     android:layout_toRightOf="@+id/log"
     android:text="@string/not" />

 <Button
     android:id="@+id/close_bracket"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/open_bracket"
     android:layout_alignBottom="@+id/open_bracket"
     android:layout_toRightOf="@+id/open_bracket"
     android:text="@string/close_bracket" />

 <Button
     android:id="@+id/open_bracket"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBaseline="@+id/squareroot"
     android:layout_alignBottom="@+id/squareroot"
     android:layout_toLeftOf="@+id/not"
     android:text="@string/open_bracket" />

 <Button
     android:id="@+id/In"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/sin"
     android:layout_toLeftOf="@+id/cos"
     android:text="@string/In" />

 <Button
     android:id="@+id/squareroot"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/log"
     android:layout_toLeftOf="@+id/open_bracket"
     android:text="@string/squareroot" />

</RelativeLayout>


fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
android:orientation="vertical" >

<Button
    android:id="@+id/seven"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/seven" />

<Button
    android:id="@+id/nine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/eight"
    android:text="@string/nine" />

<Button
    android:id="@+id/div"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/nine"
    android:text="@string/div" />

<Button
    android:id="@+id/eight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/four"
    android:layout_toRightOf="@+id/seven"
    android:text="@string/eight" />

<Button
    android:id="@+id/five"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/four"
    android:layout_alignBottom="@+id/four"
    android:layout_toRightOf="@+id/four"
    android:text="@string/five" />

<Button
    android:id="@+id/six"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/five"
    android:layout_toRightOf="@+id/five"
    android:text="@string/six" />

<Button
    android:id="@+id/mul"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/six"
    android:layout_toRightOf="@+id/six"
    android:text="@string/mul" />

<Button
    android:id="@+id/two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/one"
    android:layout_toRightOf="@+id/one"
    android:text="@string/two" />

<Button
    android:id="@+id/three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/six"
    android:layout_alignTop="@+id/two"
    android:text="@string/three" />

<Button
    android:id="@+id/sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/three"
    android:layout_toRightOf="@+id/three"
    android:text="@string/sub" />

<Button
    android:id="@+id/decimal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/one"
    android:text="@string/decimal" />

<Button
    android:id="@+id/zero"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/decimal"
    android:layout_toRightOf="@+id/decimal"
    android:text="@string/zero" />

<Button
    android:id="@+id/equal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/two"
    android:layout_toRightOf="@+id/two"
    android:text="@string/equal" />

<Button
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/equal"
    android:layout_toRightOf="@+id/equal"
    android:text="@string/add" />

<Button
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/five"
    android:layout_toLeftOf="@+id/five"
    android:text="@string/one" />

<Button
    android:id="@+id/four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/seven"
    android:text="@string/four" />

<Button
    android:id="@+id/cancel"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/add"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/div"
    android:text="@string/cancel" />

</RelativeLayout>


FragmentTwo.java

package com.example.fragmentstest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragmentTwo extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
  ViewGroup container, Bundle savedInstanceState) {

  // Inflate the layout for this fragment

  return inflater.inflate(
          R.layout.fragment_two, container, false);
 }
}


FragmentOne.java代码与FragmentTwo.java相同,除了类名称为FragmentOne且位于'R.layout.fragment_one'中。

最佳答案

当您创建“活动”并且对activity_main.xml进行了膨胀时,它将仅包含fragment_one.xml中的视图。目前,fragment_two.xml的视图将不存在(因为Fragment将在运行时动态添加)。这意味着findViewById(R.id.sin)将在onCreate函数中返回null,并且现在可以通过视图注册onClickListener。这就是为什么在注册ClickListener时需要try catch块的原因(以避免NullPointerExceptions)。

您应该在Fragments类而不是Activity中注册onClickListeners。尝试从Activity类中删除setOnClickListener调用,然后在活动中执行此操作。

public class FragmentTwo extends Fragment{
@Override
   public View onCreateView(LayoutInflater inflater,
  ViewGroup container, Bundle savedInstanceState) {
  // Inflate the layout for this fragment
  View view = inflater.inflate(
          R.layout.fragment_two, container, false);
  Button sin =  (Button) view.findViewById(R.id.sin);
  sin.setOnClickListener(...);
  //other onItemListeners go here
  return view;
 }
}


您可以通过调用getActivity从Fragment类访问MainActivity,并将其转换为MainActivity

((MainActivity)getActivity())

07-26 01:45