XML文件有16 EditText
用于输入8个科目和相关成绩点的分数,1 TextView
用于显示输出和1 Button
。
这是我的代码:
package com.workingbros.ak.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText s1,s2,s3,s4,s5,s6,s7,s8,g1,g2,g3,g4,g5,g6,g7,g8; //Declaring the variables of EditText widget
TextView cgpa; //Declaring the variaple for TextView widget
Button submit;// Declaring the variable for Button widget
int i;
Double sub[],gr[],fin[],res=0.0, sumg=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = findViewById(R.id.s1);
s2 = findViewById(R.id.s2);
s3 = findViewById(R.id.s3);
s4 = findViewById(R.id.s4);
s5 = findViewById(R.id.s5);
s6 = findViewById(R.id.s6);
s7 = findViewById(R.id.s7);
s8 = findViewById(R.id.s8);
g1 = findViewById(R.id.g1);
g2 = findViewById(R.id.g2);
g3 = findViewById(R.id.g3);
g4 = findViewById(R.id.g4);
g5 = findViewById(R.id.g5);
g6 = findViewById(R.id.g6);
g7 = findViewById(R.id.g7);
g8 = findViewById(R.id.g8);
submit=findViewById(R.id.submit);
cgpa= findViewById(R.id.cgpa);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (i = 1; i <= 8; i++) {
sub[i] = 0.0;
gr[i] = 0.0;
fin[i] = 0.0;
}
//Storing the value from si textfield in sub[i] which is the marks of each subject and gi textfield in gr[i] which is the grade point.
sub[1]= Double.parseDouble(s1.getText().toString());
sub[2]= Double.parseDouble(s2.getText().toString());
sub[3]= Double.parseDouble(s3.getText().toString());
sub[4]= Double.parseDouble(s4.getText().toString());
sub[5]= Double.parseDouble(s5.getText().toString());
sub[6]= Double.parseDouble(s6.getText().toString());
sub[7]= Double.parseDouble(s7.getText().toString());
sub[8]= Double.parseDouble(s8.getText().toString());
gr[1]= Double.parseDouble(g1.getText().toString());
gr[2]= Double.parseDouble(g2.getText().toString());
gr[3]= Double.parseDouble(g3.getText().toString());
gr[4]= Double.parseDouble(g4.getText().toString());
gr[5]= Double.parseDouble(g5.getText().toString());
gr[6]= Double.parseDouble(g6.getText().toString());
gr[7]= Double.parseDouble(g7.getText().toString());
gr[8]= Double.parseDouble(g8.getText().toString());
//Assigning values of marks
for(i=1;i<=8;i++) {
if(sub[i]>=90&&sub[i]<=100)
fin[i]=10.0;
else if(sub[i]>=80&&sub[i]<90)
fin[i]=9.0;
else if(sub[i]>=70&&sub[i]<80)
fin[i]=8.0;
else if(sub[i]>=60&&sub[i]<70)
fin[i]=7.0;
else if(sub[i]>=50&&sub[i]<60)
fin[i]=6.0;
else
fin[i]=0.0; // which is fail
sumg=sumg+gr[i]; // total grade point in a semester
}
for(i=1;i<=8;i++) {
if(fin[i]!=0.0)
res = res + (fin[i]*gr[i]);
}
cgpa.setText(String.valueOf(res/sumg)); // printing the value
}
});
}}
它计算CGPA。 注意:我是android studio的初学者。
Logcat报告:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.workingbros.ak.myapplication, PID: 22410
java.lang.NullPointerException: Attempt to write to null array
at com.workingbros.ak.myapplication.MainActivity$1.onClick(MainActivity.java:71)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:1)
您必须初始化 <div id="launches">
<section class="launch" v-for="launch in launches">
<h3 class="rocket">{{launch.rocket.name}}</h3>
<p>Rocket: {{launch.rocket.name}}</p>
<p>LSP: {{launch.lsp}}</p>
<p>Webcast: {{launch.vidURLs[0]}}</p>
<p>NET: {{launch.net}}</p>
<p>Location: {{launch.location.name}}</p>
</section>
</div>
<script type="text/javascript">
const vm = new Vue({
el: '#launches',
data: {
launches: []
},
mounted() {
this.getLaunches();
},
methods: {
getLaunches() {
axios.get('https://launchlibrary.net/1.3/launch?next=5&mode=verbose')
.then((response) => {
this.launches = response.data.launches;
console.log(this.launches);
})
.catch(error => {console.log(error); });
}
}
});
</script>
数组。您正在为不同的sub[],gr[],fin[]
对象设置值。 Double
是一个容器,没有初始化数组,不为其元素分配内存。在Array
setContentView(R.layout.activity_main);