0

请我需要一些帮助,我对 android 越来越疯狂。在这里你有mi代码和logcat drop,我希望你能给我指路。

为了更好地理解,我正在寻找的是;在活动“concejos”内部,有滑动选项卡,每个选项卡都是一个类似片段的布局,在这些布局内部有按钮,它们将打开一个带有图像、文本和关闭按钮的对话框片段。

代码

public class Concejos extends FragmentActivity implements ActionBar.TabListener {

AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_concejos);

    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    final ActionBar actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new SectionFragmentCocina();
            case 1:
                return new SectionFragmentComedor();
            case 2:
                return new SectionFragmentCuartoLavado();
            case 3:
                return new SectionFragmentBano();
            default:
                return new SectionFragmentCocina();
         }  
    }

    @Override
    public int getCount() {
        return 4;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return "Cocina";
        case 1:
            return "Comedor";
        case 2:
            return "Cuarto de lavado";
        case 3:
            return "Baño";
        }
        return null;
    }
}


public static class SectionFragmentCocina extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_cocina, container, false);
        // Demonstration of a collection-browsing activity.
        rootView.findViewById(R.id.btnLavavajilla).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = null;
                /*Intent intent = new Intent(getActivity(), SectionFragment1.class);
                startActivity(intent);*/
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.fragment_dialog);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.txtTexto);
                text.setText("Android custom dialog example!");
                ImageView image = (ImageView) dialog.findViewById(R.id.ivImagen);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });
        return rootView;
     }
}


public static class SectionFragmentComedor extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_comedor, container, false);
        // Demonstration of a collection-browsing activity.
        /*rootView.findViewById(R.id.btnLavavajilla).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SectionFragment2.class);
                startActivity(intent);
            }
        });*/
        return rootView;
     }
}

public static class SectionFragmentCuartoLavado extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_cuarto_lavado, container, false);
        // Demonstration of a collection-browsing activity.
        /*rootView.findViewById(R.id.btnLavavajilla).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SectionFragment3.class);
                startActivity(intent);
            }
        });*/
        return rootView;
     }
}

public static class SectionFragmentBano extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bano, container, false);
        // Demonstration of a collection-browsing activity.
        /*rootView.findViewById(R.id.btnLavavajilla).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SectionFragment4.class);
                startActivity(intent);
            }
        });*/
        return rootView;
     }
}

}

LOGCAT

06-04 10:05:59.111: D/dalvikvm(560): GC_FOR_ALLOC freed 99K, 3% free 6972K/7171K, paused 133ms
06-04 10:05:59.151: I/dalvikvm-heap(560): Grow heap (frag case) to 8.370MB for 1536016-byte allocation
06-04 10:05:59.331: D/dalvikvm(560): GC_CONCURRENT freed 1K, 3% free 8471K/8711K, paused 9ms+35ms
06-04 10:06:00.581: D/gralloc_goldfish(560): Emulator without GPU emulation detected.
06-04 10:06:00.781: W/TextLayoutCache(560): computeValuesWithHarfbuzz -- need to force to single run
06-04 10:07:37.460: D/dalvikvm(560): GC_CONCURRENT freed 20K, 2% free 8923K/9095K, paused 10ms+53ms
06-04 10:07:42.911: D/dalvikvm(560): GC_FOR_ALLOC freed 99K, 3% free 8965K/9223K, paused 72ms
06-04 10:07:42.962: I/dalvikvm-heap(560): Grow heap (frag case) to 10.071MB for 1280016-byte allocation
06-04 10:07:43.261: D/dalvikvm(560): GC_CONCURRENT freed 9K, 3% free 10205K/10503K, paused 45ms+8ms
06-04 10:07:43.912: D/dalvikvm(560): GC_FOR_ALLOC freed 4K, 3% free 10377K/10631K, paused 93ms
06-04 10:07:43.941: I/dalvikvm-heap(560): Grow heap (frag case) to 11.451MB for 1280016-byte allocation
06-04 10:07:44.183: D/dalvikvm(560): GC_CONCURRENT freed <1K, 3% free 11626K/11911K, paused 8ms+9ms
06-04 10:07:45.182: W/TextLayoutCache(560): computeValuesWithHarfbuzz -- need to force to single run
06-04 10:07:48.961: D/AndroidRuntime(560): Shutting down VM
06-04 10:07:48.961: W/dalvikvm(560): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
06-04 10:07:49.001: E/AndroidRuntime(560): FATAL EXCEPTION: main
06-04 10:07:49.001: E/AndroidRuntime(560): java.lang.NullPointerException
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.app.Dialog.<init>(Dialog.java:149)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.app.Dialog.<init>(Dialog.java:127)
06-04 10:07:49.001: E/AndroidRuntime(560):  at com.astrixapp.Concejos$SectionFragmentCocina$1.onClick(Concejos.java:123)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.view.View.performClick(View.java:3480)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.view.View$PerformClick.run(View.java:13983)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.os.Handler.handleCallback(Handler.java:605)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.os.Looper.loop(Looper.java:137)
06-04 10:07:49.001: E/AndroidRuntime(560):  at android.app.ActivityThread.main(ActivityThread.java:4340)
06-04 10:07:49.001: E/AndroidRuntime(560):  at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:07:49.001: E/AndroidRuntime(560):  at java.lang.reflect.Method.invoke(Method.java:511)
06-04 10:07:49.001: E/AndroidRuntime(560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 10:07:49.001: E/AndroidRuntime(560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 10:07:49.001: E/AndroidRuntime(560):  at dalvik.system.NativeStart.main(Native Method)
06-04 10:07:53.230: I/Process(560): Sending signal. PID: 560 SIG: 9

PD:我尽我所能发布,如果有更好的方法请告诉我,我感谢你的帮助,对不起,我的英语。

4

1 回答 1

1

It seems you set your context variable to null and then you create an AlertDialog with that variable. So, it is giving you a NullPointerException. Instead of doing:

Context context = null;

try doing this:

Context context = SectionFragmentCocina.this.getActivity();
于 2013-06-04T15:33:29.760 回答