在对话框中查看寻呼机不工作

时间:2016-04-20 12:53:09

标签: android android-fragments android-studio android-viewpager

当我试图将查看寻呼机放入对话框时,会出现像 IllegalArgumentException 这样的例外情况。

这是我发现的例外:

 04-20 17:54:20.579 21212-21212/com.example.user.sampleproject
 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.user.sampleproject, PID: 21212
 java.lang.IllegalArgumentException: No view found for id 0x7f0c009e
 (com.example.user.sampleproject:id/pager) for fragment
 PagerFragments{747684c #0 id=0x7f0c009e android:switcher:2131493022:1}
 at

android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)

下面是我在使用DialogWithPager实现AppCompatActivity的代码。

package com.example.user.sampleproject.activity;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.user.sampleproject.R;
import com.example.user.sampleproject.fragments.PagerFragments;

import java.util.ArrayList;

public class DialogWithPager extends AppCompatActivity {

    Context context;
    ArrayList<String> tittles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tittles = new ArrayList<>();

        tittles.add("sai");
        tittles.add("rama");
        tittles.add("usha");
        tittles.add("venkataramana");
        context = DialogWithPager.this;

        LinearLayout linearLayout = new LinearLayout(context);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        linearLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        linearLayout.setLayoutParams(params);
        linearLayout.setGravity(Gravity.CENTER);

        Button button = new Button(context);
        button.setText("click");
        button.setTextColor(ContextCompat.getColor(context, R.color.background));
        button.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showdialog();
            }
        });
        linearLayout.addView(button);
        setContentView(linearLayout);

    }

    private void showdialog() {

        Dialog dialog = new Dialog(context);

        dialog.setContentView(R.layout.dialog_viewpager);

        ViewPager pager = (ViewPager) dialog.findViewById(R.id.pager);

        dialog.show();

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return new PagerFragments("hi");
            }

            @Override
            public int getCount() {
                return tittles.size();
            }
        });
    }
}












 package com.example.user.sampleproject.fragments;


    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import com.example.user.sampleproject.R;

    import java.util.ArrayList;
    import java.util.Arrays;


    /**
     * A simple {@link Fragment} subclass.
     */
    @SuppressLint("ValidFragment")
    public class PagerFragments extends Fragment {




        @SuppressLint("ValidFragment")
        public PagerFragments(String mealPlansDomain) {
            // Required empty public constructor


        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View mealplansView = inflater.inflate(R.layout.activity_alligned_text_view, null);


            return mealplansView;
        }


    }

1 个答案:

答案 0 :(得分:0)

试试这个

private void showdialog() {

            Dialog dialog = new Dialog(context);

            dialog.setContentView(R.layout.dialog_viewpager);

            ViewPager pager = (ViewPager) dialog.findViewById(R.id.pager);

            pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return new PagerFragments("hi");
                }

                @Override
                public int getCount() {
                    return tittles.size();
                }
            });

         dialog.show();
        }