2013-04-19 52 views
0

我是android開發新手。我正在關注此link以創建警報對話框。 但我收到NoClassDefFound誤差在當使用對話框片段時NoClassDef發現異常

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

編輯:堆棧跟蹤

04-19 06:40:08.640: E/AndroidRuntime(1656): FATAL EXCEPTION: 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): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.os.Handler.dispatchMessage(Handler.java:99) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.os.Looper.loop(Looper.java:150) 04-19 06:40:08.640: E/AndroidRuntime(1656): at android.app.ActivityThread.main(ActivityThread.java:4389) 04-19 06:40:08.640: E/AndroidRuntime(1656): at java.lang.reflect.Method.invokeNative(Native Method) 04-19 06:40:08.640: E/AndroidRuntime(1656): at java.lang.reflect.Method.invoke(Method.java:507) 04-19 06:40:08.640: E/AndroidRuntime(1656): at 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(); 
     } 
    } 
+0

清單中的'minSdk'和'targetSdk'? –

+0

minSdk 8,targetSdk 17,我在libs中包含了android-support-v4.jar。 – robert

+0

你可以發佈stacktrace嗎? –

回答

1

如果您正在使用minSdk=8添加支持庫是不夠的。您應該相應地使用片段的東西,你的情況:

  • 您自定義的片斷應該從android.support.v4.app.DialogFragment
  • 繼承你的活動應該從android.support.v4.app.FragmentActivity
  • 繼承你應該通過android.support.v4.app.FragmentActivity.getSupportFragmentManager()
檢索支持 FragmentManager實例
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"); 
} 
+0

其實這是不正確的。 'DialogFragment'管理所有轉換的東西,它只需要一個'FragmentManager'來顯示。 –

+0

我明白了....看來我錯了..我會自己嘗試一下。感謝您的更正:) – buptcoder

+0

您可以在這裏查看自己:http://android-developers.blogspot.it/2012/05/using-dialogfragments.html –

1

的NoClassDefFound錯誤使我相信您使用的是HC預裝置,這意味着它不具備原生支持片段。您需要使用android支持庫,並使用FragmentActivity的getSupportFragmentManager()

看看這個頁面:Using the Support Library

+0

是的,我使用的是前置HC設備,但我有增加了支持庫。不知道是什麼問題。在Android 4.2.2目標它的作品。 – robert

相關問題