2017-04-14 14 views
0

나는 안드로이드 스튜디오에 비교적 익숙하며 대화 상자에 문제가 있습니다. 응용 프로그램이 내 profile_activity에 설치된 후 한 번 대화 상자를 표시하려고합니다. 그러나 Main Activity 후에 MainActivity와 같은 컨텍스트와 관련이 있다고 생각합니다. 컨텍스트를 정적 클래스로 전달합니다. 누군가가 왜 그런지 설명 할 수 있고 이것을 고칠 수있는 방법이 있다면 정말 감사 할 것입니다. 이것은 profile_activity 확장 MainActivity입니다 :안드로이드 대화 상자 하나 대신 2 가지 활동으로 표시

public class MainActivity extends profile_activity { 
private static Context mContext; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mContext = this.getApplicationContext(); 
    //---Setting property for greeting message and styling it a bit---// 
    TextView textOut; 
    textOut = (TextView) findViewById(R.id.textView2); 
    textOut.setTextColor(Color.parseColor("#FFFFFF")); 
    textOut.setGravity(Gravity.CENTER); 
    textOut.setTextSize(getResources().getDimension(R.dimen.textsize)); 
    textOut.setTypeface(null, Typeface.BOLD_ITALIC); 
    textOut.setText("Hello " + editText.getText() + ", pick one of the zodiac symbols below " + 
      "to find out what the stars are holding for you today!"); 
    //---Initiallize properties for all of the buttons used in the Main Activity Page ---/ 
    ImageButton aquaButton; 
    aquaButton = (ImageButton) findViewById(R.id.bAquarius); 
    ImageButton piscesButton; 
    piscesButton = (ImageButton) findViewById(R.id.bPisces); 
    ImageButton ariesButton; 
    ariesButton = (ImageButton) findViewById(R.id.bAries); 
    ImageButton taurusButton; 
    taurusButton = (ImageButton) findViewById(R.id.bTaurs); 
    ImageButton geminiButton; 
    geminiButton = (ImageButton) findViewById(R.id.bGemini); 
    ImageButton cancerButton; 
    cancerButton = (ImageButton) findViewById(R.id.bCancer); 
    ImageButton leoButton; 
    leoButton = (ImageButton) findViewById(R.id.bLeo); 
    ImageButton virgoButton; 
    virgoButton = (ImageButton) findViewById(R.id.bVirgo); 
    ImageButton libraButton; 
    libraButton = (ImageButton) findViewById(R.id.bLibra); 
    ImageButton scorpioButton; 
    scorpioButton = (ImageButton) findViewById(R.id.bScorpio); 
    ImageButton sagittariusButton; 
    sagittariusButton = (ImageButton) findViewById(R.id.bSagittarius); 
    ImageButton capriButton; 
    capriButton = (ImageButton) findViewById(R.id.bCapricorn); 

    //---Setting actions for each button when pressed---/ 

    aquaButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent aquaActivity = new Intent(MainActivity.this, 
        aqua_Activity.class); 
      startActivity(aquaActivity); 
     } 
    }); 
    piscesButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent piscesActivity = new Intent(MainActivity.this, 
        pisces_activity.class); 
      startActivity(piscesActivity); 
     } 
    }); 
    ariesButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent ariesActivity = new Intent(MainActivity.this, 
        aries_activity.class); 
      startActivity(ariesActivity); 
     } 
    }); 
    taurusButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent taurusActivity = new Intent(MainActivity.this, 
        taurus_activity.class); 
      startActivity(taurusActivity); 
     } 
    }); 
    geminiButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent geminiActivity = new Intent(MainActivity.this, 
        gemini_activity.class); 
      startActivity(geminiActivity); 
     } 
    }); 
    cancerButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent cancerActivity = new Intent(MainActivity.this, 
        cancer_activity.class); 
      startActivity(cancerActivity); 
     } 
    }); 
    leoButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent leoActivity = new Intent(MainActivity.this, 
        leo_activity.class); 
      startActivity(leoActivity); 
     } 
    }); 
    virgoButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent virgoActivity = new Intent(MainActivity.this, 
        virgo_activity.class); 
      startActivity(virgoActivity); 
     } 
    }); 
    libraButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent libraActivity = new Intent(MainActivity.this, 
        libra_activity.class); 
      startActivity(libraActivity); 
     } 
    }); 
    scorpioButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent scorpioActivity = new Intent(MainActivity.this, 
        scorpio_activity.class); 
      startActivity(scorpioActivity); 
     } 
    }); 
    sagittariusButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent sagittActivity = new Intent(MainActivity.this, 
        sagittarius_activity.class); 
      startActivity(sagittActivity); 
     } 
    }); 
    capriButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent capriActivity = new Intent(MainActivity.this, 
        capricorn_activity.class); 
      startActivity(capriActivity); 
     } 
    }); 


} 
//--Initializing static methods for navigation--// 
private static void goToCapriAct() { 
    Intent capriacti = new Intent(mContext, capricorn_activity.class); 
    mContext.startActivity(capriacti); 
} 
private static void goToAquaAct() { 
    Intent aquaacti = new Intent(mContext, aqua_Activity.class); 
    mContext.startActivity(aquaacti); 
} 
private static void goToPiscAct() { 
    Intent piscacti = new Intent(mContext, pisces_activity.class); 
    mContext.startActivity(piscacti); 
} 
private static void goToAriesAct() { 
    Intent ariesacti = new Intent(mContext, aries_activity.class); 
    mContext.startActivity(ariesacti); 
} 
private static void goToTaurAct() { 
    Intent taursacti = new Intent(mContext, taurus_activity.class); 
    mContext.startActivity(taursacti); 
} 
private static void goToGeminiAct() { 
    Intent geminiacti = new Intent(mContext, gemini_activity.class); 
    mContext.startActivity(geminiacti); 
} 
private static void goToCancAct() { 
    Intent cancacti = new Intent(mContext, cancer_activity.class); 
    mContext.startActivity(cancacti); 
} 
private static void goToLeoAct() { 
    Intent leoacti = new Intent(mContext, leo_activity.class); 
    mContext.startActivity(leoacti); 
} 
private static void goToVirgoAct() { 
    Intent virgoacti = new Intent(mContext, virgo_activity.class); 
    mContext.startActivity(virgoacti); 
} 
private static void goToLibraAct() { 
    Intent libraacti = new Intent(mContext, libra_activity.class); 
    mContext.startActivity(libraacti); 
} 
private static void goToScorpAct() { 
    Intent scorpacti = new Intent(mContext, scorpio_activity.class); 
    mContext.startActivity(scorpacti); 
} 
private static void goToSaggAct() { 
    Intent saggacti = new Intent(mContext, sagittarius_activity.class); 
    mContext.startActivity(saggacti); 
} 

//--Setting DatePickerFragment--// 
public static class DatePickerFragment extends DialogFragment 
     implements DatePickerDialog.OnDateSetListener { 
    Calendar myCalendar = Calendar.getInstance(); 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 

     int year = myCalendar.get(Calendar.YEAR); 
     int month = myCalendar.get(Calendar.MONTH); 
     int day = myCalendar.get(Calendar.DAY_OF_MONTH); 

     // Create a new instance o f DatePickerDialog and return it 
     int theme; 
     if (Build.VERSION.SDK_INT < 23) theme = AlertDialog.THEME_HOLO_DARK; 
     else theme = android.R.style.Theme_Holo_Dialog; 
     return new DatePickerDialog(getActivity(), theme, this, year, month, day); 

    } 

    //--Checks about every single sign and navigation--// 
    String message; 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
     // Do something with the date chosen by the user 
     month = month + 1; 
     if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)){ 
      goToCapriAct(); 
      } 
     else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17)){ 
      goToAquaAct(); 
     } 
     else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19)) { 
      goToPiscAct(); 
     } 
     else if ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) { 
      goToAriesAct(); 
     } 
     else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) { 
      goToTaurAct(); 
     } 
     else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) { 
      goToGeminiAct(); 
     } 
     else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) { 
      goToCancAct(); 
     } 
     else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) { 
      goToLeoAct(); 
     } 
     else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) { 
      goToVirgoAct(); 
     } 
     else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) { 
      goToLibraAct(); 
     } 
     else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day >= 1 && day <= 21)) { 
      goToScorpAct();} 
     else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) { 
      goToSaggAct();} 
     else 
     { System.out.println("Illegal date");} 
     Toast toast = Toast.makeText(getContext(), message, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

public void showDatePickerDialog(View v) { 
    DialogFragment newFragment = new MainActivity.DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
} 

public View.OnClickListener exitButtonLitener = new View.OnClickListener() { 
    public void onClick(View v) { 
     notificationcall(); 
     finish(); 
     System.exit(0); 
    } 
}; 
public View.OnClickListener aboutButtonListener = new View.OnClickListener() { 
    public void onClick(View v) { 
     Intent about_activity = new Intent(MainActivity.this, 
       aboutActivity.class); 
     startActivity(about_activity); 
    } 
}; 

public void makeTag(String tag) { 
    String or = savedname.getString(tag, null); 
    SharedPreferences.Editor preferencesEditor = savedname.edit(); 
    preferencesEditor.putString("tag", tag); 
    preferencesEditor.apply(); 
} 

public View.OnClickListener saveButtonListener = new View.OnClickListener() { 
    public void onClick(View v) { 
     //---Setting requirements for editText property---// 
     if (editText.getText().length() > 0) { 
      makeTag(editText.getText().toString()); 
      ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) 
        .hideSoftInputFromWindow(editText.getWindowToken(), 0); 
      Intent main_activity = new Intent(MainActivity.this, 
        MainActivity.class); 
      startActivity(main_activity); 
     } 


    } 
};} 

을 여기에 profile_activity입니다 : 당신이 super.OnCreate() 전화를 가지고있는 onCreate() 방법 안에 당신의 MainActivity 클래스에서

public class profile_activity extends AppCompatActivity { 
//---Setting public proerty for variables inherited in Main Activity---// 
private Context cnt; 
private String m_Text = ""; 
public EditText editText; 
SharedPreferences savedname; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile_activity); 
    cnt = this; 

    if(isFirstTime()){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(profile_activity.this); 
    builder.setTitle("Title"); 

    // Set up the input 
    final EditText input = new EditText(cnt); 
    // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text 
    input.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 
    builder.setView(input); 

    // Set up the buttons 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      m_Text = input.getText().toString(); 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    builder.show();} 

    //---Initiallizing the properties used in profile activity---// 
    editText = (EditText) findViewById(R.id.name); 
    savedname = getSharedPreferences("notes", MODE_PRIVATE); 
    ImageButton btn1 = (ImageButton) findViewById(R.id.btn1); 
    ImageButton btn2 = (ImageButton) findViewById(R.id.btn2); 
    ImageButton btn3 = (ImageButton) findViewById(R.id.btn3); 

    editText.setText(savedname.getString("tag",null)); 
    //---Initiallizing functions for each button--// 
    btn1.setOnClickListener(saveButtonListener); 
    btn2.setOnClickListener(aboutButtonListener); 
    btn3.setOnClickListener(exitButtonLitener); 


} 
//---Creating public function for notifications---// 

public void notificationcall(){ 
    //---Setting parameters to the notification---// 
    NotificationCompat.Builder notifBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setDefaults(NotificationCompat.DEFAULT_ALL) 
      .setSmallIcon(R.drawable.notificaion_icon) 
      .setTicker("Alert New Message") 
      .setContentTitle("Goodbye!") 
      .setContentText("It was nice having you today! Come back tomorrow for your new horoscope!") 
      .setAutoCancel(true); 
    //---Giving funcionality to the notification---// 
    Intent resultIntent = new Intent(this, MainActivity.class); 
    //---Back stack supporting navigation of the notification---// 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    stackBuilder.addParentStack(MainActivity.class); 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    notifBuilder.setContentIntent(resultPendingIntent); 
    //---Building the application---// 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, notifBuilder.build()); 


} 

private boolean isFirstTime() 
{ 
    SharedPreferences preferences = getPreferences(MODE_PRIVATE); 
    boolean ranBefore = preferences.getBoolean("RanBefore", false); 
    if (!ranBefore) { 
     // first time 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putBoolean("RanBefore", true); 
     editor.apply(); 
    } 
    return !ranBefore; 
} 
     public View.OnClickListener exitButtonLitener = new View.OnClickListener(){ 
       public void onClick(View v){ 
        notificationcall(); 
        finish(); 
        System.exit(0); 
       } 
     }; 
     public View.OnClickListener aboutButtonListener = new View.OnClickListener(){ 
       public void onClick(View v){ 
        Intent about_activity = new Intent (profile_activity.this, 
          aboutActivity.class); 
        startActivity(about_activity); 
       } 
     }; 
     public void makeTag(String tag){ 
      String or = savedname.getString(tag, null); 
      SharedPreferences.Editor preferencesEditor = savedname.edit(); 
      preferencesEditor.putString("tag",tag); 
      preferencesEditor.apply(); 
     } 

     public View.OnClickListener saveButtonListener = new View.OnClickListener(){ 
      public void onClick(View v) { 
       //---Setting requirements for editText property---// 
       if (editText.getText().length()>0){ 
        makeTag(editText.getText().toString()); 
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)) 
          .hideSoftInputFromWindow(editText.getWindowToken(),0); 
       Intent main_activity = new Intent(profile_activity.this, 
        MainActivity.class); 
       startActivity(main_activity); 
     } 



    } 
}; 

답변

0

. 따라서 profile_activity이 호출되면 onCreate()이 실행 된 후 MainActivity 호출에서 호출됩니다. 그래서 한 번에 두 개의 대화 상자가 표시됩니다. 때문에 AlertDialog.Builder object.show()profile_activityonCreate() 안에 있습니다.

따라서 MainActivity의 onCrreate()에서 super 전화를 제거하면 문제가 해결됩니다.

+0

예.하지만 super 클래스를 제거하면 onCreate에 수퍼 클래스가 있어야한다는 오류가 발생합니다. – redberry