簡體   English   中英

Android-從片段鏈接活動?

[英]Android - Linking Activity from a Fragment?

大家好,我想讓用戶單擊一個按鈕,然后將其發送到片段中的其他活動。 但是,每次單擊該按鈕時,它都會崩潰,因為我不是一個很好的調試器,所以我想知道是否可以得到一些幫助? 這是有問題的代碼:

僅供參考,我具有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)

我記得在早期的片段實驗中遇到了麻煩。 這是我從片段中處理活動導航的方式。

家庭碎片

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;
        }
    }

家庭活動

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM