Java 自定义对话框有问题

Java 自定义对话框有问题,java,android,Java,Android,我需要一些有关自定义对话框的帮助。这是我第一次构建自定义对话框,但由于某些原因,它一直崩溃。下面的代码是我的onCreate方法中的代码。我想点击编辑文本框,弹出对话框,然后输入股票代码或股票价格,点击确定,它将填充编辑文本框。请帮忙 public void test() { // Click on Stock Price to bring up dialog box myStockPrice.setOnClickListener(new View.OnClickListener

我需要一些有关自定义对话框的帮助。这是我第一次构建自定义对话框,但由于某些原因,它一直崩溃。下面的代码是我的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();

        }
    });

使用嵌套的匿名内部类很难判断,但我认为您的带有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);

您可能希望将内部类拆分,以使所有这些内容更易于阅读和调试。

使用嵌套的匿名内部类很难判断,但我认为您的带有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);

您可能希望将内部类分解,以使所有这些内容更易于阅读和调试。

下面的代码非常有用,但是我希望自定义布局能够正常工作

谢谢

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

下面的代码工作起来很有魅力,但是我想让自定义布局工作起来

谢谢

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

logcat中的错误是什么,显示logcat.com.CSV.Buescher.options PricingCalculatorActivity$2$1.onClickOptions PricingCalculatorActivity.java:186-哪一行是行号186?//Toast.makeTextgetApplicationContext,value,logcat中的错误是什么,显示logcat.com.CSV.Buescher.options PricingCalculatorActivity$2$1.onClickoptions PricingCalculatorActivity.java:186-哪一行是行号186?//Toast.makeTextgetApplicationContext,value,上面的操作非常合理,但是我仍然收到相同的错误。我在调试时遇到了困难:上面的说明非常合理,但是我仍然收到相同的错误。我在调试时遇到了困难: