I have a problem with this code
public class AgendaActivity extends AppCompatActivity {
private DbManager db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agendalayout);
db=new DbManager(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addxml, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_add:
final Dialog d = new Dialog(this);
d.setTitle("Prova");
d.setCancelable(false);
d.setContentView(R.layout.popupdialog);
Window window = d.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.show();
//eventi Button
Button b = (Button) d.findViewById(R.id.ok_action);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText p1 = (EditText) findViewById(R.id.Ora_inizio);
EditText p2 = (EditText) findViewById(R.id.Ora_fine);
EditText p3 = (EditText) findViewById(R.id.pause);
String p1Value = p1.getText().toString();
String p2Value = p2.getText().toString();
String p3Value = p3.getText().toString();
try {
int p1Final = Integer.parseInt(p1Value);
} catch (Exception e) {
Toast exceptionAgenda = Toast.makeText(AgendaActivity.this,
"Uno dei due campi o entrambi sono vuoti!",
Toast.LENGTH_LONG);
exceptionAgenda.show();
}
//db.save(primoValore,secondoValore,terzoValore);
}
});
default:
}
return true;
};
}
}
});
Button b2=(Button) d.findViewById(R.id.canceldialog);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(d.getContext(), "cliccatocancel", Toast.LENGTH_LONG).show();
d.cancel();
}
});
The problem is when I click the button Ok_action this error: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference. I think the problem is on the line String p1Value = p1.getText().toString();
and I don’t know how to resolve. Can someone help me?
add an output error:
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: FATAL EXCEPTION: main
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: Process: com.apps.giak.lavorotime, PID: 16356
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.apps.giak.lavorotime.AgendaActivity$1.onClick(AgendaActivity.java:62)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View.performClick(View.java:5155)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20747)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5832)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteIn
XML
`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"></LinearLayout>
<EditText
android:id="@+id/Ora_inizio"
android:inputType="numberDecimal"
android:digits="0123456789./"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Ore" />
<EditText
android:id="@+id/Ora_fine"
android:inputType="date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="data"/>
<EditText
android:id="@+id/pause"
android:inputType="numberDecimal"
android:digits="0123456789./"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint=""/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/ok_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Ok"/>
<Button
android:id="@+id/canceldialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Cancel"/>
</TableRow>
`