gpt4 book ai didi

java - 当我设置类实例时,我的 Android 应用程序崩溃了

转载 作者:行者123 更新时间:2023-12-02 00:04:25 27 4
gpt4 key购买 nike

我对 Android 编程很陌生。我正在开发一个应用程序,它可以很好地打开并且几乎可以按照我想要的方式工作。但我有一个问题,我不知道解决它的方法。这就是问题所在:我正在用一个值填充 EditText,并且在我的 java 代码中该值将初始化我创建的对象。但这永远不会发生,因为任何初始化对象的尝试都会使我的应用程序崩溃。我在其中输入值以初始化对象的editText 是名为spotText 的editText。有人可以帮忙吗?这是 XML 代码:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TableLayout
android:id="@+id/TheTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3,4,5,6,7,8,9,10,11,12" >

<TableRow
android:id="@+id/tbRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/spot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/spot"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/div"
android:text="@string/div"
android:gravity="center"
android:textColor="@color/white"
android:textSize="10sp" />

<TextView
android:id="@+id/rate"
android:text="@string/Rate"
android:gravity="center"
android:textColor="@color/white"
android:textSize="10sp" />

<CheckBox
android:id="@+id/smile"
android:text="@string/UseSmile"
android:textColor="@color/white"
android:textSize="10sp"/>

<TextView
android:id="@+id/ATM"
android:text="@string/ATM"
android:gravity="center"
android:textColor="@color/white"
android:textSize="10sp" />
</TableRow>

<TableRow
android:id="@+id/tbRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<EditText
android:id="@+id/spotText"
android:hint="@string/Spot"
android:gravity="center"
android:inputType="numberSigned"
android:textColor="@color/white"
android:textSize="10sp" />

<EditText
android:id="@+id/divRate"
android:hint="@string/divRate"
android:gravity="center"
android:inputType="numberDecimal"
android:textColor="@color/white"
android:textSize="10sp" />

<EditText
android:id="@+id/IR"
android:hint="@string/IRate"
android:gravity="center"
android:inputType="numberDecimal"
android:textColor="@color/white"
android:textSize="10sp" />
<EditText
android:id="@+id/ATMVOL"
android:hint="@string/ATM"
android:layout_column="4"
android:gravity="center"
android:inputType="numberDecimal"
android:textColor="@color/white"
android:textSize="10sp" />

</TableRow>

<TableRow
android:id="@+id/tbRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/product"
android:gravity="center"
android:textSize="10sp"
android:textColor="@color/white" />


<TextView
android:id="@+id/strike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/strike"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/maturity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/maturity"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/vol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:textSize="10sp"
android:gravity="center"
android:text="@string/volatility"
android:textColor="@color/white" />

<TextView
android:id="@+id/Qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:textSize="10sp"
android:gravity="center"
android:text="@string/Quantity"
android:textColor="@color/white" />

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="15dp"
android:textSize="10sp"
android:gravity="center"
android:text="@string/price"
android:textColor="@color/white" />

<TextView
android:id="@+id/delta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/delta"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/gamma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/gamma"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/theta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/theta"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/vega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/vega"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/vanna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/vanna"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />

<TextView
android:id="@+id/vomma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="@string/vomma"
android:textSize="10sp"
android:gravity="center"
android:textColor="@color/white" />
</TableRow>
<TableRow
android:id="@+id/tbRow4" >
<Spinner
android:id="@+id/spin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dropDownSelector="@string/Product"
android:prompt="@string/Product"
android:entries="@array/Array"
android:background="@color/SpinColor"
android:textSize="10sp"/>
<EditText
android:id="@+id/K1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="K"
android:gravity="center"
android:textSize="10sp"/>
<EditText
android:id="@+id/T1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="T"
android:gravity="center"
android:textSize="10sp"/>
<EditText
android:id="@+id/Vol1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Vol"
android:gravity="center"
android:textSize="10sp"/>
<EditText
android:id="@+id/Qty1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Qty"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/Px1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/delta1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/gamma1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/theta1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/vega1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/vanna1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
<TextView
android:id="@+id/vomma1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="10sp"/>
</TableRow>

<TableRow
android:id="@+id/tbRow9">
<Button
android:id="@+id/delBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/delete"/>
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="@string/Add"/>

</TableRow>
</TableLayout>

</HorizontalScrollView>
</ScrollView>

这是MainActivity类代码

    package com.example.lilpricer;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

int nbRow = 1;

TableRow tb1 = null; TableRow tb2 = null; TableRow tb3 = null; TableRow tb4 = null;
TableRow tb5 = null; TableRow tb6 = null; TableRow tb7 = null; TableRow tb8 = null;
TableRow tb9 = null;
TextView tvVol = null; TextView tvSpot;
EditText etSpot = null; EditText etDiv = null; EditText etIR = null; EditText etVol = null;
EditText etK1= null; EditText etK2= null; EditText etK3 = null; EditText etK4= null;
EditText etMat1= null; EditText etMat2= null; EditText etMat3 = null; EditText etMat4= null;
EditText etVol1= null; EditText etVol2= null; EditText etVol3 = null; EditText etVol4= null;
EditText etQty1= null; EditText etQty2= null; EditText etQty3 = null; EditText etQty4= null;
Spinner spin1 = null; Spinner spin2 = null; Spinner spin3 = null; Spinner spin4 = null;
Button Del = null; Button Add = null; Button Price = null;
CheckBox cbSmile = null;
Stock S=null;
Future Fut1 = null; Future Fut2 = null; Future Fut3 = null; Future Fut4 = null;
Option Opt1 = null; Option Opt2 = null; Option Opt3 = null; Option Opt4 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


setWidgetRef();


tb5.setVisibility(View.INVISIBLE); tb6.setVisibility(View.INVISIBLE); tb7.setVisibility(View.INVISIBLE);
tvVol.setVisibility(View.INVISIBLE); etVol.setVisibility(View.INVISIBLE);


etSpot.addTextChangedListener(new TextWatcher() {
double TheSpot= 0;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
TheSpot=Double.parseDouble(etSpot.getText().toString());
S.SetStock(TheSpot);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}



private void setWidgetRef()
{
tb1 = (TableRow) findViewById(R.id.tbRow1); tb2 = (TableRow) findViewById(R.id.tbRow2);
tb3 = (TableRow) findViewById(R.id.tbRow3); tb4 = (TableRow) findViewById(R.id.tbRow4);
tb5 = (TableRow) findViewById(R.id.tbRow5); tb6 = (TableRow) findViewById(R.id.tbRow6);
tb7 = (TableRow) findViewById(R.id.tbRow7); tb8 = (TableRow) findViewById(R.id.tbRow8);
tb9 = (TableRow) findViewById(R.id.tbRow9);

etSpot = (EditText) findViewById(R.id.spotText); etDiv=(EditText) findViewById(R.id.divRate);
etIR= (EditText) findViewById(R.id.IR); etVol = (EditText) findViewById(R.id.ATMVOL);
etK1= (EditText) findViewById(R.id.K1); etK2= (EditText) findViewById(R.id.K2);
etK3= (EditText) findViewById(R.id.K3); etK4= (EditText) findViewById(R.id.K4);
etMat1 = (EditText) findViewById(R.id.T1);etMat2 = (EditText) findViewById(R.id.T2);
etMat3 = (EditText) findViewById(R.id.T3);etMat4 = (EditText) findViewById(R.id.T4);
etVol1 =(EditText) findViewById(R.id.Vol1);etVol2 =(EditText) findViewById(R.id.Vol2);
etVol3 =(EditText) findViewById(R.id.Vol3);etVol4 =(EditText) findViewById(R.id.Vol4);
etQty1=(EditText) findViewById(R.id.Qty1); etQty2=(EditText) findViewById(R.id.Qty2);
etQty3=(EditText) findViewById(R.id.Qty3); etQty4=(EditText) findViewById(R.id.Qty4);

tvVol = (TextView) findViewById(R.id.ATM); tvSpot = (TextView) findViewById(R.id.spot);

Del = (Button) findViewById(R.id.delBtn); Add = (Button) findViewById(R.id.addBtn);
Price = (Button) findViewById(R.id.priceBtn);

cbSmile = (CheckBox) findViewById(R.id.smile);

spin1 = (Spinner) findViewById(R.id.spin1);spin2 = (Spinner) findViewById(R.id.spin2);
spin3 = (Spinner) findViewById(R.id.spin3);spin4 = (Spinner) findViewById(R.id.spin4);
}
}

这是我尝试初始化实例的类

    package com.example.lilpricer;

public class Stock {
private double S;

Stock(double Price){
S=Math.abs(Price);
}

Stock(){
this.S=0;
}
public void SetStock(double price) {
// TODO Auto-generated method stub
S=Math.abs(price);
}
}

这是应用程序崩溃时的 logcat。正如您所看到的,它似乎是 EditText 中的 NullPointerException,其中我输入了初始化对象 Stock 的值,但我不明白为什么会出现这样的错误,因为我在 java 中定义了一个 EditText 并将其与 EditText 链接起来我的 XML 页面,与我的对象(此处名为 S)相同,它已被定义为全局变量,因此它存在。我显然在这里遗漏了一些东西,但我只是看不出它是什么。非常感谢您的帮助。

01-06 02:06:24.770: E/AndroidRuntime(5721): FATAL EXCEPTION: main
01-06 02:06:24.770: E/AndroidRuntime(5721): java.lang.NullPointerException
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.example.lilpricer.MainActivity$1.onTextChanged(MainActivity.java:144)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.TextView.sendOnTextChanged(TextView.java:7092)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.TextView.handleTextChanged(TextView.java:7151)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8794)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.TextView.doKeyDown(TextView.java:5246)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.TextView.onKeyDown(TextView.java:5065)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.View.dispatchKeyEvent(View.java:7198)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.HorizontalScrollView.dispatchKeyEvent(HorizontalScrollView.java:332)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:353)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.os.Looper.loop(Looper.java:137)
01-06 02:06:24.770: E/AndroidRuntime(5721): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-06 02:06:24.770: E/AndroidRuntime(5721): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 02:06:24.770: E/AndroidRuntime(5721): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 02:06:24.770: E/AndroidRuntime(5721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 02:06:24.770: E/AndroidRuntime(5721): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您从未实例化该类。在使用S之前添加S=new Stock()S=new Stock(price)

关于java - 当我设置类实例时,我的 Android 应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178679/

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