簡體   English   中英

自定義對話框出現問題

[英]Having Issues with Custom Dialog Box

我的自定義對話框需要幫助。 這是我第一次構建自定義對話框,由於某種原因,該對話框一直崩潰。 以下代碼是我在onCreate()方法中擁有的代碼。 我想單擊編輯文本框,彈出對話框,然后輸入股票代碼或股票價格,單擊確定,它將填充編輯文本框。 請幫忙

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}

嵌套的匿名內部類很難分辨,但我認為對話框中有ID為StockSymbol和StockPrice的小部件。 當找到它們時,您可以執行以下操作:

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

在活動而不是對話框中查找。 找不到它們,因此單擊按鈕時輸入為null。 嘗試以下方法:

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

您可能想要突破內部類,使所有這些都易於閱讀和調試。

下面的代碼很吸引人,但是我想讓自定義布局起作用。

謝謝

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM