Error when calling dialog.show "Source not found" (Android)

481 views Asked by At

I'm quite new to Android programming and now have my first problem I couldn't solve by searching.

I get a "Source not found" error when trying to show a custom alertdialog, that says "The source attachment does not contain the source for the file DialogFragment.class."

I get this error irrespective of using the android support library(and then FragmentActivity and getSupportFragmentManager) or not(then Activity and getFragmentManager).

Below is an extremly simplified code, which still produces the error. In this case it shall only call the dialog via a simple ok button click implemented in the fragment_main.xml by android:onClick="startDialogbyButton"

The MainActivity:

package com.example.testdialogue;

//import android.app.Activity;
//import android.app.DialogFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    Toast.makeText(
            this , "Achtung:...lala" , Toast.LENGTH_SHORT).show();

}

public void startDialogbyButton(View view){
    showDialogU();
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

public void showDialogU() {
    DialogFragment newFragment = new PlInputDialog();

    newFragment.show(getSupportFragmentManager(), "dialog");
}

}

and PlInputDialog:

package com.example.testdialogue;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;

public class PlInputDialog extends DialogFragment {

public PlInputDialog(){
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //int title = getArguments().getInt("title");
    AlertDialog alertdialog = null;


        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View customView = inflater.inflate(R.layout.dialog_player_input, null);

        AlertDialog.Builder PlInDia = new AlertDialog.Builder(getActivity())
            .setView(customView)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                }
            })
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        alertdialog = PlInDia.create();

    return alertdialog;

}

}

I could imagine the problem isn't the code itself but a mistake when I added the library. But given that this happens also when I'm not using the support library, I don't know where I could make a mistake using only the normal android library.

Thanks in advance.

1

There are 1 answers

1
Sam On BEST ANSWER

you are using wrong import for support package

import android.app.Fragment;

you should use

import android.support.v4.app.Fragment;

and also for getFragmentManager() use getSupportFragmentManager() instead