我正在制作应用,但我遇到了问题:
在我的MainActivity.java中只有2个按钮,当我点击它们时,它们应该改变Intent(每个按钮都有不同的意图)。
Android Studio不会给我任何错误,我无法解决。
这是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);
}
});
}
}
这是第一个意图:
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();
}
}
这是第二个意图:
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();
}
}
这是清单:
<?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>
我知道我发布了很多代码,但我无法摆脱这个问题。
答案 0 :(得分:0)
这是因为这个......
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());
由于你只是初始化a,b和c ......因为a1
为空,所以a.getText().toString()
无法获得浮点值。
答案 1 :(得分:0)
请尝试以下操作以避免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);