在我的代码中,我使用简单的editText和textView,使用editText获取值,并在main.java中进行计算。 并在textview上写下结果。
但问题是在app中运行,有
引起:java.lang.NullPointerException
...
...
com.example.jk.kktae0034_sec.MainActivity。 (MainActivity.java:52)
第52行是
TextView recText = (TextView) findViewById (R.id.rec_WH); //here is first error
...
EditText editFck = (EditText) findViewById (R.id.fck); //if first error is // than here is occur error
public class MainActivity extends AppCompatActivity {
Button btnCal;
TextView recText = (TextView)findViewById(R.id.rec_WH);
TextView wheelStr = (TextView)findViewById(R.id.wheelStr);
TextView ironStr = (TextView)findViewById(R.id.ironStr);
EditText editFck = (EditText)findViewById(R.id.fck);
EditText editFy = (EditText)findViewById(R.id.fy);
EditText edit_life_load = (EditText)findViewById(R.id.life_load);
EditText edit_dead_load = (EditText)findViewById(R.id.dead_load);
EditText editL = (EditText)findViewById(R.id.l);
EditText editRatio = (EditText)findViewById(R.id.ratio);
double b, d, roh, Mn, Mu, pi=0.85, w, L, fy, fck, roh_max, roh_min, h, a, c, beta1, strain_t, As;
double life_load, dead_load, ratio;
double AA[][] = {
{9.53,71.3,142.6,213.9,285.2,356.5,427.8,499.1,570.4,641.7,713},
{12.7,126.7,253.4,380,1,506.8,633.5,760.2,886.9,1014,1140},
{15.9,198.6,397.2,595.8,794.4,993.0,1191,1390,1589,1787,1986},
{19.1,286.5,573.0,859.5,1146,1433,1719,2006,2292,2579,2865},
{22.2,387.1,774.2,1161,1548,1935,2322,2710,3097,3484,3871},
{25.4,506.7,1013.4,1520,2027,2534,3040,3547,4054,4560,5067},
{28.6,642.4,1285,1927,2570,3212,3854,4497,5139,5782,6424},
{31.8,794.2,1588,2383,3177,3971,4765,5559,6354,7148,7942},
{34.9,956.6,1913,2870,3826,4783,5740,6696,7653,8609,9566},
{38.1,1140,2280,3420,4560,5700,6840,7980,9120,10260,11400}
};
int num[] = {10,13,16,19,22,25,29,32,35,38 };
public void settingHeight(int i){
int j, cnt=0;
h = d + 0.5 * AA[i][0] + 12.7 + 40;
for(j = 0; j < 10000; j++){
if(j*10 > h){
h = j*10;
cnt++;
break;
}
if(cnt > 0)
break;
}
d = h - (0.5 * AA[i][0] + 12.7 + 40);
}
void reSetting(){
a = As * fy / (0.85 * fck * b);
c = a / beta1;
strain_t = (d - c) / c * 0.003;
if (strain_t > 0.005)
pi = 0.85;
else if (strain_t > 0.002)
pi = 0.65 + (0.0043 - 0.002) * 200 / 3;
else
pi = 0.65;
Mn = As * fy * (d - a / 2);
Mu = pi * Mn;
recText.setText("B = "+String.format("%.2f", b)+"mm, D = "+String.format("%.2f", d)+"mm");
wheelStr.setText("Mu = "+String.format("%.2f", Mu/1000000)+"KN*m");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fck = Double.parseDouble(editFck.getText().toString());
fy = Double.parseDouble(editFy.getText().toString());
life_load = Double.parseDouble(edit_life_load.getText().toString());
dead_load = Double.parseDouble(edit_dead_load.getText().toString());
L = Double.parseDouble(editL.getText().toString());
ratio = Double.parseDouble(editRatio.getText().toString());
btnCal = (Button)findViewById(R.id.btnCal);
btnCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//steel ratio
roh = 0.85 * 0.85 * fck / fy * 0.003 / (0.003 + 0.005);
//main
w = 1.2 * dead_load + 1.6 * life_load;
Mu = w * L*L / 8;
//setting side
int i, cnt = 0;
double bdd;
bdd = Mu*1000000 / (pi*roh*fy*(1 - 0.59*roh*fy / fck));
bdd = bdd / (ratio*ratio);
b = Math.pow(bdd,0.33333333);
for (i = 1;i<=10000;i++) {
if (i * 10 > b) {
b = i * 10;
cnt++;
break;
} if (cnt > 0)
break;
}
d = b * ratio;
//setting steel
int x, y, cnt_1, cnt_2;
double Ass;
Ass = roh * b * d;
for (x = 0; x < 10; x++) {
for (y = 0; y < 10; y++) {
if (AA[x][y] >= Ass) {
roh_max = 0.85 * 0.85 * fck / fy * 0.003 / 0.007;
if (0.25 * Math.sqrt(fck) / fy > 1.4 / fy)
roh_min = 1.4 / fy;
else
roh_min = 0.25 * Math.sqrt(fck) / fy;
roh = AA[x][y] / b / d;
if (roh > roh_min && roh < roh_max) {
cnt_2 = y;
cnt_1 = x;
As = AA[x][y];
ironStr.setText("철근"+cnt_2+" -D "+num[cnt_1]);
settingHeight(cnt);
reSetting();
}
}
}
}
}
});
}
}
布局
<RelativeLayout
android:id="@+id/mainRelayout">
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input_text_Layout">
<TextView
android:id="@+id/life_load_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Life_Load"
android:textSize="15pt"/>
<TextView
android:id="@+id/dead_load_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dead_Load"
android:textSize="15pt"
android:layout_below="@+id/life_load_text"/>
<TextView
android:id="@+id/l_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="L"
android:textSize="15pt"
android:layout_below="@+id/dead_load_text"/>
<TextView
android:id="@+id/fck_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fck"
android:textSize="15pt"
android:layout_below="@+id/l_text"/>
<TextView
android:id="@+id/fy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fy"
android:textSize="15pt"
android:layout_below="@+id/fck_text"/>
<TextView
android:id="@+id/ratio_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ratio"
android:textSize="15pt"
android:layout_below="@+id/fy_text"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/input_Layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_toRightOf="@+id/input_text_Layout">
<EditText
android:id="@+id/life_load"
android:labelFor="@+id/life_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10" />
<EditText
android:id="@+id/dead_load"
android:labelFor="@+id/dead_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/life_load"/>
<EditText
android:id="@+id/l"
android:labelFor="@+id/l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/dead_load"/>
<EditText
android:id="@+id/fck"
android:labelFor="@+id/fck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/l"/>
<EditText
android:id="@+id/fy"
android:labelFor="@+id/fy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/fck"/>
<EditText
android:id="@+id/ratio"
android:labelFor="@+id/ratio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/fy"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/input_Layout"
android:id="@+id/screen_value">
<TextView
android:id="@+id/rec_WH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="width_height" />
<TextView
android:id="@+id/wheelStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="wheelStrongth"
android:layout_below="@+id/rec_WH"/>
<TextView
android:id="@+id/ironStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"
android:text="ironStrength"
android:layout_below="@id/wheelStr"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/input_Layout"
android:layout_toRightOf="@+id/screen_value">
<Button
android:id="@+id/btnCal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calculation" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_below="@+id/btnCal"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:3)
所有都应该在onCreate()..
中实例化TextView recText ,wheelStr,ironStr ;
EditText editFck ,editFy ,edit_life_load,edit_dead_load,editL,editRatio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recText = (TextView)findViewById(R.id.rec_WH);
wheelStr = (TextView)findViewById(R.id.wheelStr);
ironStr = (TextView)findViewById(R.id.ironStr);
editFck = (EditText)findViewById(R.id.fck);
editFy = (EditText)findViewById(R.id.fy);
edit_life_load = (EditText)findViewById(R.id.life_load);
edit_dead_load = (EditText)findViewById(R.id.dead_load);
editL = (EditText)findViewById(R.id.l);
editRatio = (EditText)findViewById(R.id.ratio);
.....................
...................
答案 1 :(得分:1)
您无法在onCreate()
功能
你必须在里面进行初始化。
public class MainActivity extends AppCompatActivity {
Button btnCal;
TextView recText, wheelStr, ironStr;
EditText editFck, editFy, edit_life_load, edit_dead_load, editL, editRatio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* OTHER CODE */
recText = (TextView)findViewById(R.id.rec_WH);
wheelStr = (TextView)findViewById(R.id.wheelStr);
ironStr = (TextView)findViewById(R.id.ironStr);
editFck = (EditText)findViewById(R.id.fck);
editFy = (EditText)findViewById(R.id.fy);
edit_life_load = (EditText)findViewById(R.id.life_load);
edit_dead_load = (EditText)findViewById(R.id.dead_load);
editL = (EditText)findViewById(R.id.l);
editRatio = (EditText)findViewById(R.id.ratio);
/* OTHER CODE */
}
}
答案 2 :(得分:0)
你需要在onCreate()方法中为资源提供id:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFck = (EditText)findViewById(R.id.fck);
......
}
答案 3 :(得分:0)
而不是像
public class MainActivity extends AppCompatActivity {
Button btnCal;
TextView recText = (TextView)findViewById(R.id.rec_WH);
TextView wheelStr = (TextView)findViewById(R.id.wheelStr);
TextView ironStr = (TextView)findViewById(R.id.ironStr);
EditText editFck = (EditText)findViewById(R.id.fck);
EditText editFy = (EditText)findViewById(R.id.fy);
EditText edit_life_load = (EditText)findViewById(R.id.life_load);
EditText edit_dead_load = (EditText)findViewById(R.id.dead_load);
EditText editL = (EditText)findViewById(R.id.l);
EditText editRatio = (EditText)findViewById(R.id.ratio);
您已将所有这些内容放入 onCreate
TextView recText ,wheelStr,ironStr ;
EditText editFck ,editFy ,edit_life_load,edit_dead_load,editL,editRatio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recText = (TextView)findViewById(R.id.rec_WH);
wheelStr = (TextView)findViewById(R.id.wheelStr);
ironStr = (TextView)findViewById(R.id.ironStr);
editFck = (EditText)findViewById(R.id.fck);
editFy = (EditText)findViewById(R.id.fy);
edit_life_load = (EditText)findViewById(R.id.life_load);