Reputation: 739
Hi i have an app at start show me login button, when i click this it shows me a dialog when input username and password, then i click ok and it should save username into a file in my internal storage. But when i press ok my app crashes giving me error in logcat.
Here mainclass:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loginMethod(View v){
MyDialog dialog = new MyDialog();
dialog.show(getSupportFragmentManager(),"my_dialog");
}
public void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
}
Here DialogClass:
public class MyDialog extends DialogFragment{
LayoutInflater inflater;
View v;
EditText user, password;
MainActivity main;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.my_dialog_layout,null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
user = (EditText) v.findViewById(R.id.username);
password = (EditText) v.findViewById(R.id.password);
if (user.getText().toString().equals("hello") && password.getText().toString().equals("123")) { //here it checks if username and password are "hello" and "123"; if true it should save into file
main.writeToFile(user.getText().toString());
Intent i = new Intent("user_activity");
startActivity(i);
Toast.makeText(getActivity(), "Welcome: " + user.getText().toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
}
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
This is the logcat:
01-07 10:53:32.000: E/AndroidRuntime(13341): FATAL EXCEPTION: main
01-07 10:53:32.000: E/AndroidRuntime(13341): java.lang.NullPointerException
01-07 10:53:32.000: E/AndroidRuntime(13341): at com.example.digitpassword2.MyDialog$1.onClick(MyDialog.java:44)
01-07 10:53:32.000: E/AndroidRuntime(13341): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-07 10:53:32.000: E/AndroidRuntime(13341): at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 10:53:32.000: E/AndroidRuntime(13341): at android.os.Looper.loop(Looper.java:137)
01-07 10:53:32.000: E/AndroidRuntime(13341): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-07 10:53:32.000: E/AndroidRuntime(13341): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:53:32.000: E/AndroidRuntime(13341): at java.lang.reflect.Method.invoke(Method.java:525)
01-07 10:53:32.000: E/AndroidRuntime(13341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-07 10:53:32.000: E/AndroidRuntime(13341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 10:53:32.000: E/AndroidRuntime(13341): at dalvik.system.NativeStart.main(Native Method)
Thanks
Upvotes: 0
Views: 638
Reputation: 132972
Here:
main.writeToFile(user.getText().toString());
NullPointerException
because main
instance of MainActivity
is Null
.
if you want to call method from Activity
to Fragment
then do it as:
((MainActivity)getActivity()).writeToFile(user.getText().toString());
Upvotes: 2