0

我是android开发的新手。我正在关注此链接以创建一个警报对话框。但我收到 NoClassDefFound 错误

newFragment.show(getFragmentManager(), "dialog");

编辑:堆栈跟踪

04-19 06:40:08.640: E/AndroidRuntime(1656): 致命异常: main 04-19 06:40:08.640: E/AndroidRuntime(1656): java.lang.NoClassDefFoundError: org.poovelil.gpstest.MainActivity$ MyAlertDialogFragment 04-19 06:40:08.640: E/AndroidRuntime(1656): at org.poovelil.gpstest.MainActivity.onCreate(MainActivity.java:37) 04-19 06:40:08.640: E/AndroidRuntime(1656):在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 04-19 06 :40:08.640: E/AndroidRuntime(1656): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 android.app.ActivityThread。访问$1500(ActivityThread.java:135) 04-19 06:40:08.640:E/AndroidRuntime(1656): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 android.os.Handler.dispatchMessage(Handler .java:99) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 android.os.Looper.loop(Looper.java:150) 04-19 06:40:08.640: E/AndroidRuntime(1656 ): 在 android.app.ActivityThread.main(ActivityThread.java:4389) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 java.lang.reflect.Method.invoke(Method.java:507) 04-19 06:40:08.640: E/AndroidRuntime(1656): 在 com.android .internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 04-19 06:40:08.640: E/AndroidRuntime(1656): at com.android.internal.os.ZygoteInit。main(ZygoteInit.java:607) 04-19 06:40:08.640: E/AndroidRuntime(1656): at dalvik.system.NativeStart.main(Native Method)

我的代码如下。

public class MainActivity extends Activity {

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

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean gpsOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean networkOn = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
        if(!gpsOn) {
            Toast.makeText(getApplicationContext(), "GPS Provider is off", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "GPS Provider is on", Toast.LENGTH_SHORT).show();
        } 

        if(!networkOn) {
            Toast.makeText(getApplicationContext(), "Network provider is off", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Network provider is on", Toast.LENGTH_LONG).show();
        }

        if(!networkOn || !gpsOn) {
            DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
            newFragment.show(getFragmentManager(), "dialog");
             //startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public static class MyAlertDialogFragment extends DialogFragment {

        public static MyAlertDialogFragment newInstance(int title) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putInt("title", title);
            frag.setArguments(args);
            return frag;
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");


            return new AlertDialog.Builder(getActivity())
                //.setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        //((FragmentAlertDialog) getActivity()).doPositiveClick();

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {


                    }
                })
                .create();
        }
    }
4

3 回答 3

1

NoClassDefFound 错误让我相信您使用的是 pre-HC 设备,这意味着它没有对 Fragments 的本机支持。您将需要使用 android 支持库并改用 FragmentActivity getSupportFragmentManager()

查看此页面:使用支持库

于 2013-04-19T01:35:55.023 回答
1

如果您使用minSdk=8添加支持库是不够的。您应该相应地使用片段内容,在您的情况下:

  • 您的自定义片段应该继承自android.support.v4.app.DialogFragment
  • 你的活动应该继承自android.support.v4.app.FragmentActivity
  • 您应该通过以下方式检索支持FragmentManager实例android.support.v4.app.FragmentActivity.getSupportFragmentManager()
于 2013-04-19T01:42:37.843 回答
0

您需要先将对话框添加到 FramgmentManager:

    FragmentManager fm = getFragmentManager();
    DialogFragment newFragment = null;

    final FragmentTransaction transaction = fm.beginTransaction();
    newFragment = (DialogFragment)fm.findFragmentByTag("dialog");
    if (newFragment == null) {
        newFragment = MyAlertDialogFragment.newInstance(1)
        transaction.add(0x11313123, newFragment, "dialog");
    }
   newFragment.show(getFragmentManager(), "dialog");
}
于 2013-04-19T01:27:36.573 回答