gpt4 book ai didi

java - 自定义对话框有问题

转载 作者:行者123 更新时间:2023-11-29 09:19:07 25 4
gpt4 key购买 nike

我的自定义对话框需要一些帮助。这是我第一次构建自定义对话框,但由于某种原因它一直崩溃。下面的代码是我在 onCreate() 方法中的代码。我想点击 edittext 框弹出对话框,然后输入股票代码或股票价格,点击确定,它将填充 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();

}
});

日志

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

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

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

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

关于java - 自定义对话框有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7537045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com