Android应用在实施号码选择器对话框后停止工作

时间:2015-07-15 19:09:00

标签: android android-studio dialog numberpicker

我正在尝试添加一个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);
    }
}

NumberPicker对话框页面

public class FixtureDescription extends ActionBarActivity {

    public AlertDialog.Builder dialogBuilder;
    public int btnFixtureString = 0;
    public Button btnFixture = (Button) findViewById(R.id.stepper);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_fixture_description);
        TextView textFixture = (TextView) findViewById(R.id.FixtureDescName);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String s = extras.getString("textString");
            textFixture.setText(s);
        }
        btnFixture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                stepperDialog();
            }
        });
    }



    private void stepperDialog() {
        final NumberPicker numberPicker;
        Button btnAdd;
        Button btnCancel;
        final Dialog customDialog = new Dialog(this);


        customDialog.setContentView(R.layout.numberpicker);
        customDialog.setTitle("Number of Fixtures");
        numberPicker = (NumberPicker) customDialog.findViewById(R.id.numberPicker);
        btnAdd = (Button) customDialog.findViewById(R.id.buttonAdd);
        btnCancel = (Button) customDialog.findViewById(R.id.buttonCancel);

        numberPicker.setMaxValue(100);
        numberPicker.setMinValue(0);
        btnAdd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                btnFixtureString = numberPicker.getValue();
                Display();
                customDialog.dismiss();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                customDialog.dismiss();
            }
        });
        customDialog.show();
    }

    public void Display()
    {
        btnFixture.setText(btnFixtureString);
    }
    @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_fixture_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;
        }

        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)

1 个答案:

答案 0 :(得分:4)

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

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

您应该在btnFixture内通过findViewById初始化onCreate