我的AlertDialog
框在应有的状态下没有显示。
我已经做了一些查找,看来它们可能会弹出我正在使用的片段后面。这就是我所拥有的。
我在ActionListeners之前使用的硬编码AlertDialog
只是为了检查对话框是否会弹出。我也无法使我的蓝牙图像可点击。这些问题可能是相关的。
TransferFragment.java
//some code used from
// http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/
package com.project.BluetoothTransfer_v1000;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TransferFragment extends Fragment{
private TextView filePathTextView;
private Button startTransferButton;
private ImageView bluetoothImage;
ProgressDialog transferDialog;
Handler updateBarHandler;
private static final int REQUEST_BLUETOOTH = 1;
private static final int DISCOVER_DURATION = 300;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//set the user interface layout for this activity
setRetainInstance(false);
View v = inflater.inflate(R.layout.activity_bluetooth_transfer, parent, false);
context = this.getActivity();
filePathTextView = (TextView) v.findViewById(R.id.file_path_textView);
startTransferButton = (Button) v.findViewById(R.id.start_transfer_button);
bluetoothImage = (ImageView) v.findViewById(R.id.bluetooth_imageView);
bluetoothImage.setClickable(true);
//show alert dialog to test
new AlertDialog.Builder(context)
.setTitle("Outside")
.setMessage("outside click listener")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
startTransferButton.setOnClickListener(new View.OnClickListener() {
//start transfer processes
@Override
public void onClick(View v){
//check to make sure the file path text view != null
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter != null){
if (filePathTextView.getText() != null){
enableBluetooth();
updateBarHandler = new Handler();
}
else {
//alert user to input file path
}
}//adapter check
}//anon class
});
bluetoothImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//display dialog showing program specs and creators
new AlertDialog.Builder(context).setTitle("About")
.setMessage("Created by: /nHal Walters /nChris Lawter /nRoger Shealy")
.setPositiveButton("Awesome!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
});
return v;
}
public void launchBarDialog(View view){
final Context context = this.getActivity();
transferDialog = new ProgressDialog(context);
transferDialog.setTitle("Downloading file...");
transferDialog.setMessage("Transfer in progress... ");
transferDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
transferDialog.setProgress(0);
transferDialog.setMax(20);
transferDialog.show();
new Thread(new Runnable(){
@Override
public void run(){
try {
//
while (transferDialog.getProgress() <= transferDialog.getMax()){
Thread.sleep(2000);
updateBarHandler.post(new Runnable(){
@Override
public void run(){
transferDialog.incrementProgressBy(2);
}
});
if (transferDialog.getProgress() == transferDialog.getMax()){
transferDialog.dismiss();
}//end if
}//end while
} catch (Exception e){
}
}
}).start();
}
public void enableBluetooth(){
Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoveryIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVER_DURATION);
startActivityForResult(discoveryIntent, REQUEST_BLUETOOTH);
}
}
主要活动:
package com.project.BluetoothTransfer_v1000;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class TransferActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transfer);
TransferFragment fragment = new TransferFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
}
}
最佳答案
移动这个
Context context = this.getActivity();
在
onCreateView
的TransferFragment
内部 Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//set the user interface layout for this activity
setRetainInstance(true);
Log.v(TAG, "outside");
View v = inflater.inflate(R.layout.activity_bluetooth_transfer, parent, false);
context = this.getActivity();
编辑:
public class TransferActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transfer);
TransferFragment fragment = new TransferFragment();
FragmentManager fragmentManager = getSupportFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
}
然后
activity_transfer.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
关于android - AlertDialog未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23087421/