有自定义对话框的问题

时间:2011-09-24 04:36:11

标签: java android

我的自定义对话框需要一些帮助。这是我第一次构建自定义对话框,并且由于某种原因它不断崩溃。以下代码是我在onCreate()方法中的代码。我想点击edittext框,弹出对话框,然后输入股票代码或股票价格,点击OK,它将填充edittext框。请帮忙

public void test() {
    // Click on Stock Price to bring up dialog box
    myStockPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Create the dialog
            final Dialog dialog = new Dialog(
                    OptionsPricingCalculatorActivity.this);

            // Set the content view to our xml layout
            dialog.setContentView(R.layout.enterstocksym);

            // Set the title of the dialog. this space is always drawn even
            // if blank so might as well use it
            dialog.setTitle("Ticker Symbol");

            dialog.setCancelable(true);
            // dialog.setMessage("Enter Company Ticker Symbol");

            // Here we add functionality to our dialog box's content. In
            // this example it's the two buttons

            // Set an EditText view to get user input
            input = (EditText) findViewById(R.id.StockSymbol);
            input2 = (EditText) findViewById(R.id.StockPrice);

            Button okButton = (Button) dialog.findViewById(R.id.BtnOk);

            okButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Tickervalue = input.getEditableText().toString().trim();
                    // Do something with value!

                    // Toast.makeText(getApplicationContext(), value,
                    // Toast.LENGTH_SHORT).show();
                    // Send Stock Symbol into Request
                    SoapObject request = new SoapObject(NAMESPACE,
                            METHOD_NAME);
                    request.addProperty("symbol", Tickervalue);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.setOutputSoapObject(request);
                    envelope.dotNet = true;
                    HttpTransportSE httpTransport = new HttpTransportSE(
                            SERVICE_URL);
                    // httpTransport.debug = true;
                    try {
                        httpTransport.call(SOAP_ACTION, envelope);
                        SoapPrimitive result = (SoapPrimitive) envelope
                                .getResponse();
                        parseResponse(result.toString());
                    } catch (Exception e) {
                        Log.d("STOCK",
                                e.getClass().getName() + ": "
                                        + e.getMessage());
                        Toast t = Toast.makeText(
                                OptionsPricingCalculatorActivity.this,
                                e.getClass().getName() + ": "
                                        + e.getMessage(), 10);
                        t.show();
                    }

                }

                private void parseResponse(String response)
                        throws Exception {
                    // TODO Auto-generated method stub

                    DocumentBuilderFactory dbf = DocumentBuilderFactory
                            .newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document document = db.parse(new InputSource(
                            new InputStreamReader(new ByteArrayInputStream(
                                    response.getBytes()), "UTF-8")));
                    Element element = document.getDocumentElement();
                    NodeList stocks = element.getElementsByTagName("Stock");
                    if (stocks.getLength() > 0) {
                        for (int i = 0; i < stocks.getLength();) {
                            Element stock = (Element) stocks.item(i);
                            Element Tickervalue = (Element) stock
                                    .getElementsByTagName("Last").item(0);
                            // Send data from response to OUTPUT object
                            EditText tv = (EditText) findViewById(R.id.txtStockPrice);
                            tv.setText(Tickervalue.getFirstChild()
                                    .getNodeValue());
                            break;
                        }
                    }
                }
            });

            Button cancelButton = (Button) dialog
                    .findViewById(R.id.BtnCancel);
            cancelButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog.show();

        }
    });

logcat的

09-24 04:10:51.252: ERROR/AndroidRuntime(428): FATAL EXCEPTION: main
09-24 04:10:51.252: ERROR/AndroidRuntime(428): java.lang.NullPointerException
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at com.CSV.Buescher.OptionsPricingCalculatorActivity$2$1.onClick(OptionsPricingCalculatorActivity.java:186)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.view.View.performClick(View.java:2408)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.view.View$PerformClick.run(View.java:8816)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.os.Handler.handleCallback(Handler.java:587)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.os.Looper.loop(Looper.java:123)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at java.lang.reflect.Method.invoke(Method.java:521)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-24 04:10:51.252: ERROR/AndroidRuntime(428):     at dalvik.system.NativeStart.main(Native Method)
09-24 04:10:51.282: WARN/ActivityManager(59):   Force finishing activity com.CSV.Buescher/.OptionsPricingCalculatorActivity
09-24 04:10:51.865: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{44ece368 com.CSV.Buescher/.OptionsPricingCalculatorActivity}

2 个答案:

答案 0 :(得分:0)

很难用嵌套的匿名内部类来判断,但我认为你的小部件有ids StockSymbol和StockPrice都在对话框中。当你找到它们时,你会这样做:

input = (EditText)findViewById(R.id.StockSymbol);       
input2 = (EditText)findViewById(R.id.StockPrice);

查看活动而不是对话框。它们未被找到,因此单击按钮时输入为空。试试这个:

input = (EditText)dialog.findViewById(R.id.StockSymbol);       
input2 = (EditText)dialog.findViewById(R.id.StockPrice);

您可能希望打破内部类以使所有这些更容易阅读和调试。

答案 1 :(得分:0)

下面的代码很有魅力,但是我希望自定义布局能够正常工作。

由于

LinearLayout lila1= new LinearLayout(this);
lila1.setOrientation(1); //1 is for vertical orientation
final EditText input = new EditText(this); 
final EditText input1 = new EditText(this);
lila1.addView(input);
lila1.addView(input1);
alert.setView(lila1);
相关问题