Android 警报对话框未显示
我的Android 警报对话框未显示,android,android-alertdialog,Android,Android Alertdialog,我的AlertDialog对话框在应该显示的时候没有显示 我做了一些观察,似乎它们可能会在我使用的片段后面出现。这是我的 在ActionListeners之前,我用硬编码的AlertDialog来检查对话框是否会弹出。我也有困难使我的蓝牙图像可点击。不过,这些问题可能是相关的 TransferFragment.java //some code used from // http://examples.javacodegeeks.com/android/core/ui/progres
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);
}
}
移动这个
Context context = this.getActivity();
在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();
编辑:
然后
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>
@Override
公共视图onCreateView(){
super.onCreate(savedInstanceState);
最终上下文上下文=this.getActivity()
仍然没有。仍然没有。用我现在拥有的更新了原始帖子。抱歉,对话框根本没有显示。没有编译器错误。发布xml以获取TransferFragment帮助吗?@user3494474难怪它没有显示或崩溃。因为您没有将片段附加到活动导入android.support.v4.app.fragment;
对于fragment.Removeimport android.app.fragment;
,如果我还记得我发表的评论,我想我在一个小时前就提到了这一点。是的,现在我理解了你的沮丧。我关注的是FragmentActivity类而不是TransferActivity类。很抱歉浪费你的时间。你帮了我很大的忙。
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();
}
<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>