0

대화 상자 조각을 만들었습니다. 하위 메뉴 항목을 누르면 바로이 대화 상자를 현재 활동으로 가져와야합니다. 그래서 AlertDialog.Builder 빌드를 생성하고 선언했습니다. 내 활동이 시작될 때.안드로이드 액티비티에서 Dialog Fragment 내부의 하위 메뉴 항목을 생성하는 중 오류가 발생했습니다.

public class Placed_Product_Details extends Activity { 
String usrnme,name,proid; 
AlertDialog.Builder builde; 

해당 코드는 다음과 같습니다.

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    builde = new AlertDialog.Builder(Placed_Product_Details.this); 
    LayoutInflater inflater = Placed_Product_Details.this.getLayoutInflater(); 
     // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builde.setView(inflater.inflate(R.layout.updateproductname, null)) 
    // Add action buttons 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // sign in the user ... 
       } 
      }); 

    return builde.create(); 

} 

이것은 하위 메뉴 코드의 일부입니다.

public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch (item.getItemId()) { 
    case R.id.delete: 
    { 

     //here i have the code for deleting from the database 

     break; 
    } 
    case R.id.updateproductname: 
    { 

       //here I have created a dialog and called it here. The code 
       //of the dialog is posted above 
     AlertDialog ab=builde.create(); 
     ab.show(); 
     break; 
    } 
    default: 
     break; 
    } 
    return super.onOptionsItemSelected(item); 

} 

내 프로젝트의 res/layout/폴더에 생성 된 대화 상자 updateproductname.xml의 레이아웃입니다.

하위 메뉴를 클릭하면 애플리케이션이 갑자기 닫힙니다. 왜 그런지 몰라? 나는 오류를 보여주는 logcat을 보여줄 것이다. 대화 상자 조각을 잘못 작성했기 때문입니다. 누군가 내 응용 프로그램에 오류가있는 곳을 표시 할 수 있습니까?

편집 : 여기 내 오류의 전체 로그가 ..입니다

02-17 10:50:23.267: D/AndroidRuntime(687): Shutting down VM 
02-17 10:50:23.318: W/dalvikvm(687): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-17 10:50:23.416: E/AndroidRuntime(687): FATAL EXCEPTION: main 
02-17 10:50:23.416: E/AndroidRuntime(687): java.lang.NullPointerException 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.example.onlineauction.Placed_Product_Details.onOptionsItemSelected(Placed_Product_Details.java:188) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.app.Activity.onMenuItemSelected(Activity.java:2205) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.ListView.performItemClick(ListView.java:3513) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Handler.handleCallback(Handler.java:587) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Looper.loop(Looper.java:123) 
02-17 10:50:23.416: E/AndroidRuntime(687): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-17 10:50:23.416: E/AndroidRuntime(687): at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 10:50:23.416: E/AndroidRuntime(687): at java.lang.reflect.Method.invoke(Method.java:507) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-17 10:50:23.416: E/AndroidRuntime(687): at dalvik.system.NativeStart.main(Native Method) 
02-17 10:50:27.746: I/Process(687): Sending signal. PID: 687 SIG: 9 

2 편집 : 여기에 오류를 보여주는 두 번째 로그 캣입니다. 먼저 변수 builderonCreateDialog()로 초기화되지

02-17 12:07:11.577: E/AndroidRuntime(769): FATAL EXCEPTION: main 
02-17 12:07:11.577: E/AndroidRuntime(769): java.lang.NullPointerException 
02-17 12:07:11.577: E/AndroidRuntime(769): at com.example.onlineauction.Placed_Product_Details$1.onClick(Placed_Product_Details.java:212) 
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159) 
02-17 12:07:11.577: E/AndroidRuntime(769): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-17 12:07:11.577: E/AndroidRuntime(769): at android.os.Looper.loop(Looper.java:123) 
02-17 12:07:11.577: E/AndroidRuntime(769): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-17 12:07:11.577: E/AndroidRuntime(769): at java.lang.reflect.Method.invokeNative(Native Method) 
02-17 12:07:11.577: E/AndroidRuntime(769): at java.lang.reflect.Method.invoke(Method.java:507) 
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-17 12:07:11.577: E/AndroidRuntime(769): at dalvik.system.NativeStart.main(Native Method) 
02-17 12:07:15.667: I/Process(769): Sending signal. PID: 769 SIG: 9 
+0

당신은 단지 전체 로그 대신에 그것의 일부를 게시 할 수 없습니다. – user1728071

+0

@ user1728071 전체 로그 게시 ... – njnjnj

답변

1

는 언제 어디 ...

당신은 ActivityonCreateDialog() 메서드를 재정 실제로되지 않는다는 생각을 할 수있다 호출되지 않습니다.

다음 두 가지 방법이 사용되지 않습니다 ... 참고로

protected Dialog onCreateDialog(int); 
protected Dialog onCreateDialog(int,Bundle); 

ActivityonCreateDialog()의 정의를 참조하십시오.

하지만 당신은 Activity의 메소드를 오버라이드 (override)하지 않는

protected Dialog onCreateDialog(Bundle); 

을 ... 당신은 onCreateDialog()에서 Bundle 매개 변수를 제거 할 수 있습니다

(실제로 무시되지 않았거나 알고 Override 주석을 사용) 작성 메서드를 사용하지 않는 것이 좋습니다.

public Dialog onCreateDialog() { 
    // TODO Auto-generated method stub 
    builde = new AlertDialog.Builder(Placed_Product_Details.this); 
    LayoutInflater inflater = Placed_Product_Details.this 
      .getLayoutInflater(); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    builde.setView(inflater.inflate(R.layout.updateproductname, null)) 
    // Add action buttons 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // sign in the user ... 
       } 
      }); 

    return builde.create(); 
} 

적절한 MenuItem을 클릭 할 때이 메소드를 호출

...

case R.id.updateproductname:{ 

    //here I have created a dialog and called it here. The code 
    //of the dialog is posted above 
    onCreateDialog().show(); 
    break; 
} 
+0

@ Gopal Rao. 내가 실수를했다면 용서해주세요. onCreateDialog()를 어디에서 호출하여 변수 builde를 초기화해야하는지 설명해 주시겠습니까? – njnjnj

+0

@ user1547124 내 수정 된 답변보기 ... –

+0

@ Gopal Rao. 감사. 그것은 효과가 있었다. – njnjnj