它没有将total=i1+i2+i3+i4+i5给出的值累加起来;t17.setText(String.valueOf(total));它总是在t17处显示0。

问题描述 投票:0回答:1

java的total有问题,我已经试过try和catch了,它在t17处总是显示0,看来它没有设置total值。 在t17.setText(String.valueOf(total));t17处总是显示0。

package com.example.myproject1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class Bill extends AppCompatActivity {

    TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17;
    Button b,b1;
    int total;
    int i1,i2,i3,i4,i5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        t=findViewById(R.id.textView);
        t1=findViewById(R.id.textView5);
        t2=findViewById(R.id.textView10);
        t3=findViewById(R.id.textView15);
        t4=findViewById(R.id.textView6);
        t5=findViewById(R.id.textView11);
        t6=findViewById(R.id.textView16);
        t7=findViewById(R.id.textView7);
        t8=findViewById(R.id.textView12);
        t9=findViewById(R.id.textView17);
        t10=findViewById(R.id.textView8);
        t11=findViewById(R.id.textView13);
        t12=findViewById(R.id.textView18);
        t13=findViewById(R.id.textView9);
        t14=findViewById(R.id.textView14);
        t15=findViewById(R.id.textView19);
        t16=findViewById(R.id.textView20);
        t17=findViewById(R.id.textView21);
        b=findViewById(R.id.button22);
        b1=findViewById(R.id.button23);
        Intent obj2 = getIntent();
        String r = obj2.getStringExtra("k");
        t.setText(r);
        String res = obj2.getStringExtra("key");
        String res1 = obj2.getStringExtra("key2");
        String res2 = obj2.getStringExtra("key3");
        String res3 = obj2.getStringExtra("key4");
        String res4 = obj2.getStringExtra("key5");
        String res5 = obj2.getStringExtra("key6");
        String res6 = obj2.getStringExtra("key7");
        String res7 = obj2.getStringExtra("key8");
        String res8 = obj2.getStringExtra("key9");
        String res9 = obj2.getStringExtra("key10");
        String res10 = obj2.getStringExtra("key11");
        String res11 = obj2.getStringExtra("key12");
        String res12 = obj2.getStringExtra("key13");
        String res13 = obj2.getStringExtra("key14");
        String res14 = obj2.getStringExtra("key15");
        t1.setText(res);
        t2.setText(res1);
        t3.setText(res2);
        t4.setText(res3);
        t5.setText(res4);
        t6.setText(res5);
        t7.setText(res6);
        t8.setText(res7);
        t9.setText(res8);
        t10.setText(res9);
        t11.setText(res10);
        t12.setText(res11);
        t13.setText(res12);
        t14.setText(res13);
        t15.setText(res14);
        try{
            if (res2 != null) {
                i1 = Integer.parseInt(res2);
            } else {
                i1 = 0;
            }
            if (res5 != null) {
                i2 = Integer.parseInt(res5);
            } else {
                i2 = 0;
            }
            if (res8 != null) {
                i3 = Integer.parseInt(res8);
            } else {
                i3 = 0;
            }
            if (res11 != null) {
                i4 = Integer.parseInt(res11);
            } else {
                i4 = 0;
            }
            if (res14 != null) {
                i5 = Integer.parseInt(res14);
            } else {
                i5 = 0;
            }
        }catch (NumberFormatException e){

        }
            t16.setText("Total Price:");
            total=i1+i2+i3+i4+i5;
            t17.setText(String.valueOf(total));

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent obj = new Intent(Bill.this, Menu.class);
                startActivity(obj);
            }
        });
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent obj1 = new Intent(Bill.this, MainActivity.class);
                startActivity(obj1);
            }
        });
    }

}

XML 我的代码的xml如下,虽然它与我的编码部分无关。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bill4"
    tools:context=".Bill">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="224dp"
        android:layout_marginTop="5dp"
        android:background="#92278F"
        android:gravity="center"
        android:text="user"
        android:textColor="#0066FF"
        android:textSize="24sp"
        android:textStyle="bold"
        app:fontFamily="@font/merienda_one"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="64dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="#CD81DB"
        android:fontFamily="@font/joti_one"
        android:text="Bill"
        android:textColor="#111111"
        android:textSize="@android:dimen/app_icon_size"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="60dp"
            android:background="#92278F"
            android:gravity="center"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:text="Item"
            android:textColor="#0066FF"
            android:textSize="24dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="13dp"
            android:layout_marginTop="60dp"
            android:background="#92278F"
            android:gravity="center"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:text="Quantity"
            android:textColor="#0066FF"
            android:textSize="24dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="60dp"
            android:background="#92278F"
            android:gravity="center"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:text="Price"
            android:textColor="#0066FF"
            android:textSize="24dp"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#111111"
            android:layout_marginTop="10dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#111111"
            android:layout_marginTop="10dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#111111"
            android:layout_marginTop="10dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#111111"
            android:layout_marginTop="10dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#111111"
            android:layout_marginTop="10dp"
            android:textStyle="bold"/>
    </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView10"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView11"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView12"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView13"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="120dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView15"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView16"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView17"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView18"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView19"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#111111"
                android:layout_marginTop="10dp"
                android:textStyle="bold"/>

        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/textView20"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="20dp"
            android:textColor="#111111"
            android:text=""
            android:textSize="14dp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/textView21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="20dp"
            android:textColor="#111111"
            android:textSize="14dp"
            android:textStyle="bold"
            android:text="" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="20dp"
            android:background="#92278F"
            android:textColor="#0066FF"
            android:textStyle="bold"
            app:fontFamily="@font/merienda_one"
            android:text="Cancel" />

        <Button
            android:id="@+id/button23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="165dp"
            android:layout_marginTop="20dp"
            android:background="#92278F"
            android:textColor="#0066FF"
            android:textStyle="bold"
            app:fontFamily="@font/merienda_one"
            android:text="Order" />


    </LinearLayout>


</LinearLayout>
java nullpointerexception onclick sum android-studio-3.0
1个回答
0
投票

我使用了Sharedpreferences而不是intent,但是这两种方法都可以。 数据接收类

public class class_name extends AppCompatActivity {

   SharedPreferences sharedPref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        sharedPref=this.getSharedPreferences("calis",Context.MODE_PRIVATE);
        String res = sharedPref.getString("k1",null);
        String res1 = sharedPref.getString("k2",null);
        String res2 = sharedPref.getString("k3",null);
        Integer res15 = sharedPref.getInt("k4",0);
        Integer vres15 = sharedPref.getInt("k5",0);
        int total2 = res15+vres15;
        t17.setText(String.valueOf(total2));
}
}

数据发送类

public class class_name2 extends Fragment {

    SharedPreferences sp;
    sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("k1",value1);
        editor.putInt("k4", value2);
        editor.commit();
}
© www.soinside.com 2019 - 2024. All rights reserved.