Android - 从片段链接活动?

时间:2014-10-12 03:17:42

标签: android android-fragments

嘿伙计,所以我试图让用户点击一个按钮,然后将它们从片段发送到另一个活动。但是每次点击按钮都会崩溃,现在因为我不是一个非常好的调试器,我想知道我是否能得到一些帮助?以下是有问题的代码:

仅用于引用我有Mainactivity,FragA,FragB和FragC,按钮在FraqA中。

完整代码如下所示,如果你们需要它。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;


public class FragmentA extends Fragment implements OnClickListener {


    private View v;


      public FragmentA() {
            // Required empty public constructor
        }



// Set up an interface to allow the activity to handle navigation
    public interface FragmentAListener {
        public void onNewGameClicked();

    }

    // Reference to the activity
    private FragmentAListener callBack; 


    @Override
    public void onAttach(Activity activity) {
        Log.d("FragmentA", "in onAttach");
        super.onAttach(activity);

        // Check to make sure the activity implements the listener interface.
        try {
            callBack = (FragmentAListener) activity;
        } catch (ClassCastException e) {
            Log.d("FragmentA", String.format("%s does not implement HomeFragmentListener",activity));
        }
    }

    // Infalte the view
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         Log.d("FragmentA", "in onCreateView");
            v = inflater.inflate(R.layout.fragment_a,container, false);
            configureImageButton();

            return v;
        }



    // Register view listener
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            Log.d("FragmentA", "in onViewCreated");
            super.onViewCreated(view, savedInstanceState);

            // This fragment implements on click listener, so just set to this.
            view.findViewById(R.id.Button03).setOnClickListener(this);          
        }




     // Implements OnClickListener
        @Override
        public void onClick(View v) {
            int viewId = v.getId();

            // Check what view was clicked
            switch (viewId) {
            case R.id.Button03:
                // Tell the classes implementing the listener interface
                // that the view was clicked.
                callBack.onNewGameClicked();
                break;
            default:
                break;
            }
        }






/*  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_a);

        Button Button03 = (Button) findViewById(R.id.Button03);
        Button03.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Score_activity.class);
                startActivityForResult(intent, 0); 






            }
        });

    }   */






    private void configureImageButton() {
        // TODO Auto-generated method stub
        ImageButton btn = (ImageButton) v.findViewById(R.id.imageButton1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Quack, Quack, Quack, Quack!", Toast.LENGTH_LONG).show();

            }
        });


    }




}

日志

10-12 15:16:41.393: W/dalvikvm(19248): threadid=1: thread exiting with uncaught exception (group=0x41f34ba8)
10-12 15:16:41.393: E/AndroidRuntime(19248): FATAL EXCEPTION: main
10-12 15:16:41.393: E/AndroidRuntime(19248): Process: com.example.sub_assignment1_2, PID: 19248
10-12 15:16:41.393: E/AndroidRuntime(19248): java.lang.NullPointerException
10-12 15:16:41.393: E/AndroidRuntime(19248):    at com.example.sub_assignment1_2.FragmentA.onCreate(FragmentA.java:48)
10-12 15:16:41.393: E/AndroidRuntime(19248):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
10-12 15:16:41.393: E/AndroidRuntime(19248):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
10-12 15:16:41.393: E/AndroidRuntime(19248):    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:838)
10-12 15:16:41.393: E/AndroidRuntime(19248):    at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:843)

1 个答案:

答案 0 :(得分:2)

我记得在早期的片段实验中遇到了麻烦。以下是我如何处理片段内的活动导航。

HomeFragment

public class HomeFragment extends Fragment implements OnClickListener {

    // Set up an interface to allow the activity to handle navigation.
    public interface HomeFragmentListener {
        public void onNewGameClicked();
    }

    // Reference to the activity
    private HomeFragmentListener callback; 

    @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setRetainInstance(true);      
    }

    @Override
    public void onAttach(Activity activity) {
        Log.d("HomeFragment", "in onAttach");
        super.onAttach(activity);

        // Check to make sure the activity implements the listener interface.
        try {
            callBack = (HomeFragmentListener) activity;
        } catch (ClassCastException e) {
            Log.d("HomeFragment", String.format("%s does not implement HomeFragmentListener",activity));
        }
    }

    // Inflate the view
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        Log.d("HomeFragment", "in onCreateView");
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    // Register view listener
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Log.d("HomeFragment", "in onViewCreated");
        super.onViewCreated(view, savedInstanceState);

        // This fragment implements on click listener, so just set to this.
        view.findViewById(R.id.new_game_button).setOnClickListener(this);

        // Add other view events
        view.findViewById(R.id.imageButton1).setOnClickListener(this);         
    }

    // Implements OnClickListener
    @Override
    public void onClick(View v) {
        int viewId = v.getId();

        // Check what view was clicked
        switch (viewId) {
        case R.id.new_game_button:
            // Tell the classes implementing the listener interface
            // that the view was clicked.
            callBack.onNewGameClicked();
            break;
        // More button listeners here
        case R.id.imageButton1:
            Toast.makeText(getActivity(), "Quack, Quack, Quack, Quack!", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
    }

HomeActivity

public class HomeActivity extends FragmentActivity implements HomeFragmentListener {

    // Implement the HomeFragmentListener interface.
    @Override
    public void onNewGameClicked() {
        Bundle extras = new Bundle();
        Intent intent = new Intent(this, GameActivity.class);
        intent.putExtras(extras);
        startActivity(intent);
    }