Android应用程序在实现数字选择器对话框后停止工作

Android应用程序在实现数字选择器对话框后停止工作,android,android-studio,dialog,numberpicker,Android,Android Studio,Dialog,Numberpicker,我正在尝试将一个NumberPicker作为对话框添加到我的活动中,该对话框通过按下按钮打开。现在,当我尝试使用NumberPicker对话框从一个活动移动到另一个活动时,应用程序停止工作。我不明白调试错误想告诉我什么,有什么帮助吗 NumberPicker对话框前的页面 public class RoomDescription extends ActionBarActivity { public AlertDialog.Builder dialogBuilder; publi

我正在尝试将一个
NumberPicker
作为对话框添加到我的活动中,该对话框通过按下按钮打开。现在,当我尝试使用
NumberPicker
对话框从一个活动移动到另一个活动时,应用程序停止工作。我不明白调试错误想告诉我什么,有什么帮助吗

NumberPicker
对话框前的页面

public class RoomDescription extends ActionBarActivity {

    public AlertDialog.Builder dialogBuilder;
    public List<String> myFixtures = new ArrayList<String>();
    public String listItem;

    private void setFixtureName()
    {
        dialogBuilder = new AlertDialog.Builder(this);
        final EditText txtInput = new EditText(this);


        dialogBuilder.setTitle("New Fixture");
        dialogBuilder.setMessage("What is the fixture name?");
        dialogBuilder.setView(txtInput);
        dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myFixtures.add(txtInput.getText().toString());
                populateListView();
                Toast.makeText(getApplicationContext(), "Fixture has been added.", Toast.LENGTH_SHORT).show();
            }
        });
        dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });

        AlertDialog dialogFixtureName = dialogBuilder.create();
        dialogFixtureName.show();
    }

    private void removeFixture()
    {
        dialogBuilder = new AlertDialog.Builder(this);
        final EditText txtInput = new EditText(this);

        dialogBuilder.setTitle("Select Fixture to Remove");
        dialogBuilder.setSingleChoiceItems(myFixtures.toArray(new String[myFixtures.size()]), -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                myFixtures.remove(which);
                populateListView();
                Toast.makeText(getApplicationContext(), "Fixture has been removed.", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        AlertDialog dialogFixtureName = dialogBuilder.create();
        dialogFixtureName.show();
    }
    private void populateListView()
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.items, myFixtures);

        ListView list = (ListView) findViewById(R.id.FixtureList);
        list.setAdapter(adapter);
    }

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

        populateListView();

        ListView list = (ListView) findViewById(R.id.FixtureList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    default:
                        Intent i = new Intent(RoomDescription.this, FixtureDescription.class);
                        TextView textItem = (TextView) view;
                        String textString = textItem.getText().toString();
                        i.putExtra("textString",textString);
                        startActivity(i);
                        break;
                }

            }

        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        switch (item.getItemId())
        {
            case R.id.action_settings:
                return true;
            case R.id.newFixture:
                setFixtureName();
                break;
            case R.id.removeFixture:
                removeFixture();
        }


        return super.onOptionsItemSelected(item);
    }
}
调试错误报告

07-15 13:59:27.065  11951-11951/com.customledsupply.ledaudit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.customledsupply.ledaudit, PID: 11951
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.customledsupply.ledaudit/com.customledsupply.ledaudit.FixtureDescription}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.Activity.findViewById(Activity.java:2072)
            at com.customledsupply.ledaudit.FixtureDescription.<init>(FixtureDescription.java:21)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1606)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-15 13:59:27.065 11951-11951/com.customledsupply.ledudit E/AndroidRuntime﹕ 致命异常:主
流程:com.customledsupply.ledudit,PID:11951
java.lang.RuntimeException:无法实例化activity ComponentInfo{com.customledsupply.ledaudit/com.customledsupply.ledaudit.FixtureDescription}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.view.Window.findViewById(int)”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5257)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.view android.view.Window.findViewById(int)”
在android.app.Activity.findviewbyd(Activity.java:2072)上
位于com.customledsupply.ledudit.FixtureDescription.(FixtureDescription.java:21)
位于java.lang.reflect.Constructor.newInstance(本机方法)
位于java.lang.Class.newInstance(Class.java:1606)
位于android.app.Instrumentation.newActivity(Instrumentation.java:1066)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5257)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

不要像这样初始化此变量:

public Button btnFixture = (Button) findViewById(R.id.stepper);

您应该在您的
onCreate
中通过
findViewById
初始化
btnFixture
不要像这样初始化此变量:

public Button btnFixture = (Button) findViewById(R.id.stepper);
您应该在
onCreate
中通过
findViewById
初始化
btnFixture