MPAndroidChart Intent 传递数据错误空对象引用

MPAndroidChart Intent Passing Data Error Null Object References

其实我不是程序员,但我有一个任务要完成。我问过身边的人,也看过其他的问题,但还是无法在工作中落实。 好吧,我要制作一个可以计算列的程序。 所以我在第一个activity上计算它,然后将结果传递给另一个activity。 在第二个 activity 中,我想显示一个散点图,使用 MPAndroid 图表。 我已经用随机数尝试了我的代码,它起作用了。但是当我传递数据时,应用程序崩溃了, 这是我的 logcat :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
    at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
    at java.lang.Float.parseFloat(Float.java:451)
    at com.example.diagraminteraksikolom.hasill1.scatterChartDataSet(hasill1.java:56)
    at com.example.diagraminteraksikolom.hasill1.onCreate(hasill1.java:38)
    at android.app.Activity.performCreate(Activity.java:7896)
    at android.app.Activity.performCreate(Activity.java:7885)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1353)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3530)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3699) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:230) 
    at android.app.ActivityThread.main(ActivityThread.java:7873) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034) 

这是我的第二个 activity :

public class hasill1 extends AppCompatActivity  {
private static final String TAG = "hasill1";

private ScatterChart mScatter;
private String strpnmaks, strmntek, strpntek,strmnsei,strpnsei,strmntar,strpntar,strmnnull;
private Float pnmaks,mntek,pntek,mnsei,pnsei,mntar,pntar,mnnull;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hasill1);


    mScatter = findViewById(R.id.scatter);
    ScatterDataSet scatterDataSet = new ScatterDataSet(scatterChartDataSet(),"data set");
    ArrayList<IScatterDataSet> iScatterDataSets=new ArrayList<>();
    iScatterDataSets.add(scatterDataSet);

    ScatterData scatterData= new ScatterData(iScatterDataSets);
    mScatter.setData(scatterData);
    mScatter.invalidate();

}
    private ArrayList<Entry> scatterChartDataSet() {
    ArrayList<Entry> dataSet = new ArrayList<Entry>();

        Intent intent= getIntent();
        strpnmaks=intent.getStringExtra("pnmaks");
        pnmaks=Float.parseFloat(strpnmaks);


        strmntek=intent.getStringExtra("mntek");
        mntek=Float.parseFloat(strmntek);

        strpntek=intent.getStringExtra("pntek");
        pntek=Float.parseFloat(strpntek);

        strmnsei=intent.getStringExtra("mnsei");
        mnsei=Float.parseFloat(strmnsei);

        strpnsei=intent.getStringExtra("pnsei");
        pnsei=Float.parseFloat(strpnsei);

        strmntar=intent.getStringExtra("mntar");
        mntar=Float.parseFloat(strmntar);

        strpntar=intent.getStringExtra("pntar");
        pntar=Float.parseFloat(strpntar);

        strmnnull=intent.getStringExtra("mnnull");
        mnnull=Float.parseFloat(strmnnull);

        dataSet.add(new Entry(0,pnmaks));
        dataSet.add(new Entry(mntek,pntek));
        dataSet.add(new Entry(mnsei,pnsei));
        dataSet.add(new Entry(mntar,pntar));
        dataSet.add(new Entry(mnnull,0));

        return dataSet;
}

}

*已编辑 这是我传递给第二个 activity

的第一个 activity 结果
public class data extends AppCompatActivity {
private Button hhitung;
private EditText hkolom,bkolom,ds,dsa,fc,fy,ntulangantarik,ntulangantekan,dtulangan,Mu,Pu;
private Double beta1,Ag,epsy,As1,As2,Ast,Po,Pnmaks,cb,
        ctek,atek,zstek,zatek,fstek,fstekaks,cctek,cstek,tstek,
        pntek,vtek,wtek,xtek,mntek,asei,zssei,zasei,fssei,fsseiaks,
        ccsei,cssei,tssei,pnsei,mnsei,vsei,wsei,xsei,
        ctar,atar,zstar,zatar,fstar,fstaraks,cctar,cstar,tstar,pntar,mntar,
        vtar,wtar,xtar,anull,aminleh,amaksleh,pnull,qnull,anullsqrt,
        fsnull,mncnull,mnsnull,mnnull,mdnull,anullpakai,d,Muu,Puu,MnMaks,PnNull;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data);




    hhitung = (Button) findViewById(R.id.hhitung);
    hkolom = (EditText) findViewById(R.id.hkolom);
    bkolom= (EditText) findViewById(R.id.bkolom) ;
    ds = (EditText) findViewById(R.id.ds);
    dsa = (EditText) findViewById(R.id.dsa);
    fc = (EditText) findViewById(R.id.fc);
    fy = (EditText) findViewById(R.id.fy);
    ntulangantarik = (EditText) findViewById(R.id.ntulangantarik);
    ntulangantekan = (EditText) findViewById(R.id.ntulangantekan);
    dtulangan = (EditText) findViewById(R.id.dtulangan);
    Mu = (EditText) findViewById(R.id.Mu);
    Pu = (EditText) findViewById(R.id.Pu);




    hhitung.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if ((hkolom.getText().length() > 0) && (bkolom.getText().length() > 0) &&
                    (ds.getText().length() > 0) && (dsa.getText().length() > 0)&&
                    (fc.getText().length() > 0) && (fy.getText().length() > 0) &&
                    (ntulangantarik.getText().length() > 0) &&(ntulangantekan.getText().length() > 0)&&
                    (dtulangan.getText().length() > 0) && (Mu.getText().length() > 0) &&
                    (Pu.getText().length() > 0)) {

                Double vhkolom = Double.parseDouble(hkolom.getText().toString());
                Double vbkolom = Double.parseDouble(bkolom.getText().toString());
                Double vds = Double.parseDouble(ds.getText().toString());
                Double vdsa = Double.parseDouble(dsa.getText().toString());
                Double vfc = Double.parseDouble(fc.getText().toString());
                Double vfy = Double.parseDouble(fy.getText().toString());
                Double vntulangantarik = Double.parseDouble(ntulangantarik.getText().toString());
                Double vntulangantekan = Double.parseDouble(ntulangantekan.getText().toString());
                Double vdtulangan = Double.parseDouble(dtulangan.getText().toString());
                Double vMu = Double.parseDouble(Mu.getText().toString());
                Double vPu = Double.parseDouble(Pu.getText().toString());

                if ((vfc < 30)) {
                    beta1 = 0.85;
                } else {
                    beta1 = 0.85 - 0.008 * (vfc - 30);
                }

                //Luas Penampang
                epsy = vfy / 200000;
                Ag = vbkolom * vhkolom;
                As1 = vntulangantarik * Math.pow(vdtulangan,2) * Math.PI /4;
                As2 = vntulangantekan * Math.pow(vdtulangan,2)* Math.PI /4;
                Ast = As1 + As2;

                //Tinjauan Beban Sentris
                Po = 0.85 * (vfc/1000) * (Ag - Ast) + Ast * (vfy/1000);
                Pnmaks = 0.8 * Po;

                //Kondisi Tekan Menentukan (tek)
                d= vhkolom-vds;
                cb = (600*d)/(600+ vfy);
                ctek = cb + 70;
                atek = beta1 * ctek;
                zstek = ((vhkolom - ctek) - vds) / ctek * 0.003;
                if ((zstek < epsy)) {
                    fstek = zstek * 200;
                } else {
                    fstek = vfy / 1000;
                }
                zatek = (ctek - vdsa) / ctek * 0.003;
                if ((zatek < epsy)) {
                    fstekaks = zatek * 200;
                } else {
                    fstekaks = vfy / 1000;
                }
                cctek = 0.85 * (vfc / 1000) * atek * vbkolom;
                cstek = As2 * fstekaks;
                tstek = As1 * fstek;
                pntek = cctek + cstek - tstek;
                vtek = cctek * (vhkolom / 2 - atek / 2);
                wtek = cstek * (vhkolom / 2 - vdsa);
                xtek = tstek * (vhkolom / 2 - vds);
                mntek = (vtek + wtek + xtek) / 1000;

                //Kondisi Seimbang (sei)
                asei = beta1 * cb;
                zssei = ((vhkolom - cb) - vds) / cb * 0.003;
                fssei = vfy/1000;
                zasei = (cb - vdsa) / cb * 0.003;
                if ((zasei < epsy)) {
                    fsseiaks = zasei * 200;
                } else {
                    fsseiaks = vfy/1000;
                }
                ccsei = 0.85 * (vfc / 1000) * asei * vbkolom;
                cssei = As2 * fsseiaks;
                tssei = As1 * fssei;
                pnsei = ccsei + cssei - tssei;
                vsei = ccsei * (vhkolom / 2 - asei / 2);
                wsei = cssei * (vhkolom / 2 - vdsa);
                xsei = tssei * (vhkolom / 2 - vds);
                mnsei = (vsei + wsei + xsei)/1000;

                //Kondisi Tarik Menentukan (tar)
                ctar = cb - 70;
                atar = beta1 * ctar;
                zstar = ((vhkolom - ctar) - vds) / ctar * 0.003;
                fstar = vfy / 1000;
                zatar = (ctar - vdsa) / ctar * 0.003;
                fstaraks = vfy / 1000;
                cctar = 0.85 * (vfc/1000) * atar * vbkolom;
                cstar = As2 * fstaraks;
                tstar = As1 * fstar;
                pntar = cctar + cstar - tstar;
                vtar = cctar * (vhkolom / 2 - atar / 2);
                wtar = cstar * (vhkolom / 2 - vdsa);
                xtar = tstar * (vhkolom / 2 - vds);
                mntar = (vtar + wtar + xtar)/1000;

                //Kondisi Beban Pn=0
                anull = ((As1 - As2) * (vfy/1000)) / 0.85 * (vfc/1000) * vbkolom;
                aminleh = (600 * beta1 * vdsa) / (600 - (vfy/1000));
                amaksleh = (600 * beta1 * vds) / (600 + (vfy/1000));
                pnull = (600 * As2 - As1 * vfy) / (1.7 * vfc * vbkolom);
                qnull = (600 * beta1 * As2 * vdsa) / (0.85 * vfc * vbkolom);
                anullsqrt = Math.sqrt(pnull * pnull + qnull) - pnull;
                if ((anull >= aminleh)) {
                    anullpakai = anull;
                } else {
                    anullpakai = anullsqrt;
                }
                fsnull = 600 * ((anullpakai - beta1 * vdsa) / anullpakai);
                mncnull = 0.85 * vfc * anullpakai * vbkolom * (d - anullpakai / 2);
                mnsnull = As2 * fsnull * (d - vdsa);
                mnnull = (mncnull + mnsnull) / 1000000;
                mdnull = 0.9 * mnnull;


                Muu=vMu;
                Puu=vPu;

                Intent intent= new Intent(data.this, hasill1.class);
                intent.putExtra("pnmaks",Pnmaks);
                intent.putExtra("pntek",pntek);
                intent.putExtra("mntek",mntek);
                intent.putExtra("pnsei",pnsei);
                intent.putExtra("mnsei",mnsei);
                intent.putExtra("pntar",pntar);
                intent.putExtra("mntar",mntar);
                intent.putExtra("mnnull",mnnull);
                intent.putExtra("Mu",Muu);
                intent.putExtra("Pu",Puu);
                intent.putExtra("PnNull",PnNull);
                intent.putExtra("MnMaks",MnMaks);

                startActivity(intent);



            } else {
                Toast.makeText(data.this, "MAAF DATA MASIH KOSONG", Toast.LENGTH_LONG);


            }
        }
    });

}

}

我不明白,我曾尝试研究解决方案,但我仍然坚持下去。 我希望有人可以帮助我解决它。我真的很感激,谢谢你

从您的 logcat,我可以看出您正在使用 Float.parseFloat() 来解析空字符串。我可以看看你的意图是如何传递给这个 activity 的吗?

您可以像这样从您来自的 activity 传递您的意图 - 确保将 formerActivity 替换为您来自的 activity 的名称:

Intent i = new Intent(formerActivity.this, hasill1.class);
i.putExtra("pnmaks", "50.0" (or whatever string you are passing));
i.putExtra("mntek", "60.0");

如果你要通过双打,那么你就这样做,

i.putExtra("pnmaks", 50.0);

并且您对要传递到 hasill1 class 的所有字符串执行相同的操作。然后当你完成后,你可以通过传递意图来启动你的散点图所在的 activity。

startActivity(i);

然后在 hasill1 activity 中,您可以通过以下方式获取您的意图数据:

Intent intent = getIntent();
String strpnmaks = intent.getStringExtra("pnmaks");
String strmntek = intent.getStringExtra("mntek");

要获得双倍价值,您可以这样做:

Double pnmaks = intent.getDoubleExtra("pnmaks", 0.0);

在解析 Float.parseFloat() 中的字符串之前,请确保它不为空并且是字符串数据类型的小数。例如,如果 strpnmaks 为空 - "" 或文本 - "boy",则它不能被解析为 float。它必须是字符串类型的数字格式,如“30.0”被解析为浮点数。

然后您可以检查以确保您的字符串不为空并像这样解析它:

if (strpnmaks != null) {
   Float pnmaks = Float.parseFloat(strpnmaks);
}

因为你从第一个 activity 传递一个双精度,我建议像我提出的那样从 hasill1 activity 把它作为一个双精度。