显示警告对话框时出错

时间:2015-05-10 08:05:21

标签: android dialogfragment

当我显示警告对话框时,我收到NullPointerException,但是我不知道如何解决它。 事情是它工作了几次但现在发生了一些事情。  这是logcat:

05-10 10:59:34.937  16886-16886/com.android.tfl.traffidriveapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.android.tfl.traffidriveapp.ParameterAlert.onCreateDialog(ParameterAlert.java:62)
            at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
            at android.app.BackStackRecord.run(BackStackRecord.java:622)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4424)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

我的主要活动:

package com.android.tfl.traffidriveapp;



public class MainActivity extends Activity implements ServiceConnection{
    private IExtMessage binding=null;
    private parseParametersTask task=null;
    HashMap<Integer,List<String>> parameters;
    ParameterAlert alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectToService();
        task=new parseParametersTask();
        task.execute();
        parameters=ReadParams.params;





    }

    public void connectToService(){
        Intent implicit=new Intent(IExtMessage.class.getName());
        List<ResolveInfo> matches=getPackageManager().queryIntentServices(implicit, 0);
        if(matches.size()==0){
            Toast.makeText(getApplicationContext(), "Cannot find a matching service!", Toast.LENGTH_LONG).show();
        }
        else if (matches.size()>1){
            Toast.makeText(getApplicationContext(), "Found multiple matching services!", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(getApplicationContext(), "Found the Protocol Service", Toast.LENGTH_LONG).show();
            Intent explicit=new Intent(implicit);
            ServiceInfo svcInfo=matches.get(0).serviceInfo;
            ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName, svcInfo.name);
            explicit.setComponent(cn);
            bindService(explicit, this, Context.BIND_AUTO_CREATE);



        }
    }

    public void buttonClick(View v) {
        if (v.getId() == R.id.navigation) {
            DialogFragment dialog = new navigationChooserFragment();
            dialog.show(getFragmentManager(), "navigation");
        }
        if(v.getId()==R.id.ptt){
            PackageManager pm=getPackageManager();
            Intent i=pm.getLaunchIntentForPackage("com.loudtalks");
            i.addCategory(Intent.CATEGORY_LAUNCHER);
            startActivity(i);
        }

    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d("SERVICE", "Service Connected");
        binding=IExtMessage.Stub.asInterface(service);
        try {
            binding.getMessage(cb);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d("SERVICE","Service disconnected :(");
        binding=null;

    }

    ICallBackMessage.Stub cb=new ICallBackMessage.Stub() {

        @Override
        public void onMessageReceived(int parameter,int state) throws RemoteException {
            Log.d("SERVICE","received parameter "+parameter);
            Log.d("SERVICE", "received state " + state);
            if(state==1){
                alert=new ParameterAlert();
                Bundle bundleParam=new Bundle();
                bundleParam.putInt("parameter",parameter);
                alert.setArguments(bundleParam);
                alert.show(getFragmentManager(),"alert");
            }else {
                if (alert.isVisible()) {
                    alert.dismiss();
                }
            }
        }
    };

    class parseParametersTask extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {
            ReadParams readParams =new ReadParams();
            try {
                readParams.setInputFile(getAssets().open("parameters.xls"));
                readParams.read();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

}

我的对话片段:

public class ParameterAlert extends DialogFragment implements DialogInterface.OnClickListener {
    private View form=null;
    TextView alertTitle;
    TextView alertType;
    TextView alertDetails;
    private int parameter;
    List<String> paramValues;

    public ParameterAlert(){
        //blank
    }



    @Override
    public void onResume() {
        int width=600;
        int height=400;
        super.onResume();
        Window window=getDialog().getWindow();
        window.setLayout(width, height);
        window.setGravity(Gravity.CENTER_HORIZONTAL);
        window.setGravity(Gravity.CENTER_VERTICAL);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(false);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //form=getActivity().getLayoutInflater().inflate(R.layout.alerts,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        LayoutInflater inflater=getActivity().getLayoutInflater();
        View form=inflater.inflate(R.layout.alerts,null);
        builder.setView(form);
        builder.setTitle(paramValues.get(0));
        builder.setNegativeButton(R.string.cancel_alert,this);

        Bundle bundleParam=getArguments();
        int parameter=bundleParam.getInt("parameter");
        paramValues=ReadParams.params.get(parameter);
        alertTitle= (TextView) form.findViewById(R.id.alert_title);
        //alertType= (TextView) form.findViewById(R.id.alert_type);
        alertDetails= (TextView) form.findViewById(R.id.alert_details);
        //alertType.setText(paramValues.get(0));
        alertTitle.setText(paramValues.get(1));
        alertDetails.setText(paramValues.get(2));


        return builder.create();


    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
}

1 个答案:

答案 0 :(得分:1)

 builder.setTitle(paramValues.get(0));

您在初始化之前访问paramValues,它发生在下面5行。如果我在你身边,我会避免保留包含你想要显示的数据的静态引用。您正在使用DialogFragment的子类,您应该使用setArguments / getArguments对来处理您需要的数据