2017-11-10 3 views
0

방금 ​​Android 프로그래밍 학습을 시작하여 문제가 발생했습니다 : My alert dialog's don't show up.Android : AlertDialog가 표시되지 않습니다

내 아이디어 : 앱이 실행되면 기기가 인터넷에 연결되어 있는지 자동으로 확인하고 (경고 대화 상자로) 제안합니다. 나는 많은 튜토리얼을보고 있었기 때문에

은 정말, 어떤 하나의 솔루션을 가지고, 희망, 그러나 모든 경우는 onClickListener (버튼)

코드 조각으로 시작 코드이었다

public class SplashActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    checkConnection(); 

    Intent intent = new Intent(this, MainActivity.class); 
    startActivity(intent); 
    finish(); 
} 


void checkConnection() { 
    final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this); 

    if (wifi.isConnectedOrConnecting()) { 
     // Do nothing 
    } else if (mobile.isConnectedOrConnecting()) { 

     connectionAlert.setMessage("We recommend to use wifi, enable it?"); 
     connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show(); 
      } 
     }); 

     connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 

     connectionAlert.show(); 

    } else { 

     connectionAlert.setMessage("Please, enable internet connection!"); 
     connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show(); 
      } 
     }); 

     connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       Toast.makeText(SplashActivity.this,"Mobile Data has been enabled!",Toast.LENGTH_LONG).show(); 
      } 
     }); 

     connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish(); 
      } 
     }); 

     connectionAlert.show(); 

    } 
} 

}

+0

Wi-Fi에 연결하지 않습니까? 가장 먼저 할 일은'if - else if - else' 부분을 주석 처리하고 경고가 아무런 조건없이 잘 보이는지 확인하는 것입니다. 그것의 잘 작동하는 경우에, 당신은 아마 경보가 보이지 않는 곳에 첫번째 if에 떨어지고있다. 왜 'finish()'라고 부르니? 그것을 제거하고 'Activity'를 변경하기 전에 경고가 올바르게 표시되는지 확인하십시오. – MatPag

+1

1. 예, 무선 랜에 연결되어 있지 않습니다. 2. 모두 "토스트"로 작동했습니다. 3. 튜토리얼 때문에 finish()를 사용하고있었습니다. (그것을 제거, 아무것도 변경). 4.나는이 코드를 복사하여 MainActivity (작동)에 붙여 넣으면 알 수있다. – Crelix

+0

'checkContext (R.layout.your_layout)'을 호출하여'checkConnection()'을 호출하기 전에'SplashActivity'에 대한 레이아웃을 설정해 보았습니까? – MatPag

답변

1

컨텍스트를 추가하십시오.

public class SplashActivity extends AppCompatActivity { 

private Context context; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    context = this; 

    checkConnection(); 

    Intent intent = new Intent(this, MainActivity.class); 
    startActivity(intent); 
    finish(); 
} 

///////

변경이 라인 :

AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this); 

사람 : 당신이 시작 활동을 마무리 호출하기 때문에

AlertDialog.Builder connectionAlert = new AlertDialog.Builder(context); 
+0

SplashActivity의 onCreate 내에서 레이아웃을 부 풀리십시오. super.onCreate (savedInstanceState) 아래에서 수행하십시오. 매니페스트 파일에 새 활동을 등록하십시오 (아직없는 경우). – HaroldSer

0

내가 문제를 생각한다 ! 경고 메시지를 표시해야하는 이유는 무엇입니까?

YourActivity 원래 여기

제거 할 수 있습니다 추가 된 윈도우 [email protected]를 유출 한 발견 할 것이다 당신의 로그 캣을 확인 활동을 끝내고 활동 이동 작업을 대화 상자 버튼 동작 중 하나로 이동하십시오.

0

SplashActivity 컨텍스트를 사용하여 AlertDialog을 만들고 있지만 AlertDialog이 속한 컨텍스트가 더 이상 존재하지 않으면 문제가 발생하는 것입니다.

이 메시지를 표시 할 때 Enable을 선택하기 전에 두 번째 활동 인 MainActivity으로 이동하려는 것을 고려하십시오. We recommend to use wifi, enable it?. 다음과 같이 할 수 있습니다.

public class SplashActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     checkConnection(); 
    } 


    void checkConnection() { 
     final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
     final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

     AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this); 

     if (wifi.isConnectedOrConnecting()) { 
      // Do nothing 
     } else if (mobile.isConnectedOrConnecting()) { 

      connectionAlert.setMessage("We recommend to use wifi, enable it?"); 
      connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show(); 
        Intent intent = new Intent(SplashActivity.this, MainActivity.class); 
        startActivity(intent); 
        finish(); 
       } 
      }); 

      connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 

      connectionAlert.show(); 

     } else { 

      connectionAlert.setMessage("Please, enable internet connection!"); 
      connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show(); 
       } 
      }); 

      connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        Toast.makeText(SplashActivity.this, "Mobile Data has been enabled!", Toast.LENGTH_LONG).show(); 
       } 
      }); 

      connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
       } 
      }); 

      connectionAlert.show(); 

     } 
    } 
} 

여기에는 많은 옵션이 있습니다. 하지만 중요한 것은 실제로 AlertDialog을 보여주고 있지만 다른 활동을 시작하고 SplashActivity을 끝내고 AlertDialog이 처음 구체화 된 컨텍스트 (this)를 파괴 했으므로이를 볼 수 없다는 것입니다. new AlertDialog.Builder(this);.

관련 문제