简体   繁体   中英

Android app crashes when changing intent

I'm making an app but I have a problem:

In my MainActivity.java there are only 2 buttons, and when I click them, they should change Intent (every button has a different intent).

Android Studio doesn't give me any error and I can't solve it.

Here's the MainActivity.java

package com.valeriodesiati.graph2;
import android.content.*;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
Button b1, b2; //primo e secondo grado
Intent intent;
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toast.makeText(MainActivity.this, "Benvenuto!", Toast.LENGTH_LONG).show();

    b1 = findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick (View v) {
            Intent i = new Intent(MainActivity.this, PrimoGrado.class);
            startActivity(i);
        }
    });

    b2 = findViewById(R.id.button2);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            Intent i = new Intent(MainActivity.this, SecondoGrado.class);
            startActivity(i);
        }
    });
}
}

Here's the first Intent:

package com.valeriodesiati.graph2;

import android.content.DialogInterface;
import android.content.*;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PrimoGrado extends AppCompatActivity {
    private EditText a,b;
    private Button graficoButtonPrimoGrado;
    //float a1,b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.primogrado);
    a = findViewById(R.id.a);
    b = findViewById(R.id.b);
    final float a1 = Float.parseFloat(a.getText().toString());
    final float b1 = Float.parseFloat(b.getText().toString());

    open2();

    graficoButtonPrimoGrado = findViewById(R.id.graficoButtonPrimoGrado);
    graficoButtonPrimoGrado.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(a1 == 0) {
                open(v);
                Intent iHome = new Intent(PrimoGrado.this, PrimoGrado.class);
                startActivity(iHome);
            }
            Intent i = new Intent(PrimoGrado.this, PrimoGradoGrafico.class);
            i.putExtra("a", String.valueOf(a));
            i.putExtra("b", String.valueOf(b));
            startActivity(i);
        }
    });
}

public void open(View view){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Se la a della retta è uguale a 0 è impossibile costruire il grafico.");
            alertDialogBuilder.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(PrimoGrado.this,"Riprova",Toast.LENGTH_LONG).show();
                        }
                    });

    alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

public void open2 (){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Ricorda: inserisci i dati dell'equazione anche se sono uguali a 0");
    alertDialogBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(PrimoGrado.this,"",Toast.LENGTH_LONG).show();
                }
            });

    alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}
}

Here's the second Intent:

package com.valeriodesiati.graph2;

import android.content.DialogInterface;
import android.content.*;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SecondoGrado extends AppCompatActivity {
    private EditText a,b,c;
    private Button graficoButtonSecondoGrado;
    //float a1,b1,c1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondogrado);

        open2();

        a = findViewById(R.id.a);
        b = findViewById(R.id.b);
        c = findViewById(R.id.c);
        final float a1 = Float.parseFloat(a.getText().toString());
        final float b1 = Float.parseFloat(b.getText().toString());
        final float c1 = Float.parseFloat(c.getText().toString());

        graficoButtonSecondoGrado = findViewById(R.id.graficoButtonSecondoGrado);
        graficoButtonSecondoGrado.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(a1 == 0) {
                    open(v);
                }
                Intent i = new Intent(SecondoGrado.this, SecondoGradoGrafico.class);
                i.putExtra("a", String.valueOf(a));
                i.putExtra("b", String.valueOf(b));
                i.putExtra("c", String.valueOf(c));
                startActivity(i);
            }
        });

    }

    public void open(View view){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Se la a dell'equazione è uguale a 0 la parabola perderebbe il suo senso in quanto si ridurrebbe ad una retta");
        alertDialogBuilder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(SecondoGrado.this,"Riprova",Toast.LENGTH_LONG).show();
                    }
                });

        alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    public void open2 (){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Ricorda: inserisci i dati dell'equazione anche se sono uguali a 0");
        alertDialogBuilder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(SecondoGrado.this,"",Toast.LENGTH_LONG).show();
                    }
                });

        alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

Here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.valeriodesiati.graph2">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name="com.valeriodesiati.graph2.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.valeriodesiati.graph2.PrimoGrado"
                android:label="Primo grado"
                android:parentActivityName="com.valeriodesiati.graph2.MainActivity">

            </activity>

            <activity
                android:name="com.valeriodesiati.graph2.SecondoGrado"
                android:label="Primo grado"
                android:parentActivityName="com.valeriodesiati.graph2.MainActivity">

            </activity>

            <activity
                android:name="com.valeriodesiati.graph2.PrimoGradoGrafico"
                android:label="Primo grado"
                android:parentActivityName="com.valeriodesiati.graph2.PrimoGrado">

            </activity>

            <activity
                android:name="com.valeriodesiati.graph2.SecondoGradoGrafico"
                android:label="Primo grado"
                android:parentActivityName="com.valeriodesiati.graph2.SecondoGrado">

            </activity>

        </application>

    </manifest>

I know that I posted a lot of code, but I can't really get out of this problem.

This is happening because of this...

a = findViewById(R.id.a);
b = findViewById(R.id.b);
c = findViewById(R.id.c);
final float a1 = Float.parseFloat(a.getText().toString());
final float b1 = Float.parseFloat(b.getText().toString());
final float c1 = Float.parseFloat(c.getText().toString());

Since you JUST inialized a, b and c...you can not get a float value for a1 since a.getText().toString() is null.

Try the following to avoid the NullPointerException :

float getFloatFromTextView(TextView textView) {
    if (textView != null) {
       try {
          return Float.parseFloat(textView.getText().toString());
       } catch(Throwable t) {
       }
    }
    return 0f;
} 

...
a = findViewById(R.id.a);
b = findViewById(R.id.b);
c = findViewById(R.id.c);
final float a1 = getFloatFromTextView(a);
final float b1 = getFloatFromTextView(b); 
final float c1 = getFloatFromTextView(c);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM