AlertDialog没有显示

时间:2014-04-15 14:57:21

标签: android alertdialog

我的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);
    }
}

MainActivity:

    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();
    }
}

1 个答案:

答案 0 :(得分:1)

移动此

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>