1

我正在尝试在我的应用程序中使用 AlertDialog,但它需要输入。所以我被迫在我的自定义 DialogFragment 类中创建一个接口并覆盖 onAttach() 方法。一旦我在 Dialog 类和 MainActivity 中正确设置了所有内容,我就尝试运行我的应用程序,它给了我一个运行时异常,指的是 nullpointer 等等。我不知道为什么会这样,我需要帮助,请帮助。下面你将介绍我的接口方法的 Override,我的 .show() 方法被调用的地方,我的 DialogFragment 类的代码,最后是我的错误日志。

public class MainActivity extends FragmentActivity implements CDia_exp.NDListener
{

    //Integers and Strings for performing calculations
        int a;
        int b;
        static int deci_cnt;
        int cnt;
        int temp;
        Double exp_x;
        Double exp;
        Double [] num_trk;
        String [] op_trk;
        String num_hold;
        String op_hold;
        String del_hold;

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


    //....Lots of other code here.......

   //Exp_x Button
            opSet[6].setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    if(num_hold != "")
                    {
                        cDia_exp.show(getSupportFragmentManager(), "exp_x");
                        del_hold = tViews[0].getText().toString();
                        for(int x = num_hold.length() - 1; x >= 0; x--)
                        {
                            del_hold = del_hold.substring(0, del_hold.length() - 1);
                        }
                        num_hold = exp.toString();
                        tViews[0].setText(del_hold + num_hold);
                        return;
                    }
                    else
                    {
                    return;
                    }
                }
            });
    }

    //Objects and Overrides for calling foreign functions
        CTrim cTrim = new CTrim();
        CDia cDia = new CDia();
        CDia_exp cDia_exp = new CDia_exp();

        EditText exp_inp = (EditText) findViewById(R.id.exp_inp);

        @Override
        public void onDPClick(DialogFragment dialog)
        {
            exp = Double.parseDouble(num_hold);
            exp_x = Double.parseDouble(exp_inp.getText().toString());
            for(double x = exp_x; x > 0; x--)
            {
                exp *= exp;
            }

        }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的自定义 DialogFragment 类

package com.example.musicalc;

import android.app.Activity;
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;


public class CDia_exp extends DialogFragment

{

    public interface NDListener
    {
        public void onDPClick(DialogFragment dialog);
    } 


    NDListener expListener;


    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        try
        {
            expListener = (NDListener) activity;
        }
        catch(ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + "does not implement NDListener");
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {

        AlertDialog.Builder aDia = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        aDia.setView(inflater.inflate(R.layout.calc_dia, null)).setPositiveButton(R.string.diaOk_b, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                expListener.onDPClick(CDia_exp.this);
            }
        }).setNegativeButton(R.string.cancel_b, new DialogInterface.OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int id) 
            {
                getDialog().cancel();
            }
        });


        return aDia.create();

    }
}

现在是错误日志

07-20 14:23:43.599: E/Trace(24173): error opening trace file: No such file or directory (2)
07-20 14:23:43.622: D/AndroidRuntime(24173): Shutting down VM
07-20 14:23:43.622: W/dalvikvm(24173): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:23:43.653: E/AndroidRuntime(24173): FATAL EXCEPTION: main
07-20 14:23:43.653: E/AndroidRuntime(24173): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.os.Looper.loop(Looper.java:137)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at dalvik.system.NativeStart.main(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173): Caused by: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at com.example.musicalc.MainActivity.<init>(MainActivity.java:43)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at java.lang.Class.newInstance(Class.java:1319)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:23:43.653: E/AndroidRuntime(24173):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:23:43.653: E/AndroidRuntime(24173):    ... 11 more
07-20 14:25:56.817: E/Trace(24308): error opening trace file: No such file or directory (2)
07-20 14:25:56.934: D/AndroidRuntime(24308): Shutting down VM
07-20 14:25:56.934: W/dalvikvm(24308): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:25:57.013: E/AndroidRuntime(24308): FATAL EXCEPTION: main
07-20 14:25:57.013: E/AndroidRuntime(24308): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.os.Looper.loop(Looper.java:137)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at dalvik.system.NativeStart.main(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308): Caused by: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at com.example.musicalc.MainActivity.<init>(MainActivity.java:803)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at java.lang.Class.newInstance(Class.java:1319)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:25:57.013: E/AndroidRuntime(24308):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:25:57.013: E/AndroidRuntime(24308):    ... 11 more
4

1 回答 1

0

感谢对stackoverflow的一些实验和一些研究,我找到了答案。

问题是编译器在定位视图时遇到问题,要么是因为它位于我的 activity_main.xml 文件的单独 xml 文件中,要么是因为我在 MainActiviy.java 的单独类中创建我的对话框。

为了解决这个问题,我在 onCreate 方法之外的 MainActivity.java 类中创建了我的对话框。我还创建了一个布局充气器来在我的 MainActivity 的视图中充气我的布局,这是关键,因为 LayoutInflater 提供了一种将我的 EditText 直接调用到代码中的方法。

声明我的 EditText 时修复空指针的代码如下:

LayoutInflater inflater = LayoutInflater.from(this);

//Inflater "inflates" layout into View as a value

final View infV = inflater.inflate(R.layout.calc_dia, null);

//Use layout value inflated in View "infV" to find EditText 

final EditText eDTemp = (EditText) infV.findViewById(R.id.expInp);

***对话框的完整代码如下***

public void showCD()
            {
                LayoutInflater inflater = LayoutInflater.from(this);
                final View infV = inflater.inflate(R.layout.calc_dia, null);
                final EditText eDTemp = (EditText)infV.findViewById(R.id.expInp);
                TextView tView0 = (TextView)findViewById(R.id.calcView);
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setView(infV).setPositiveButton("Ok", new     
                DialogInterface.OnClickListener() 
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                }}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        dialog.cancel();
                    }
                });

                AlertDialog alert = builder.create();
                builder.show();
            }

当您准备好出现对话框时,只需调用包含它的函数即可。

于 2014-07-21T04:07:21.850 回答