The application seems to crash when I try and get the text from the EditText:
package com.example.helloandroid;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
public class MatrixMultiply extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
openNewGameDialog();
break;
case R.id.exit_button:
finish();
break;
// More buttons go here (if any) ...
}
}
private static final String TAG = "Matrix";
private void openNewGameDialog() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.text, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Matrices");
alert.setMessage("Please enter the size of the matrix");
// Set an EditText view to get user input
alert.setView(textEntryView);
AlertDialog matrixSize = alert.create();
final EditText height1 = (EditText) matrixSize.findViewById(R.id.h1);
final EditText width1 = (EditText) MatrixMultiply.this.findViewById(R.id.w1);
final EditText height2 = (EditText) MatrixMultiply.this.findViewById(R.id.h2);
final EditText width2 = (EditText) MatrixMultiply.this.findViewById(R.id.w2);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String h1 = height1.getText().toString();
String w1 = width1.getText().toString();
String h2 = height2.getText().toString();
String w2 = width2.getText().toString();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
private void startGame(int i) { Log.d(TAG, "clicked on " + i); // Start game here...
}
}
it will be crashing due to a thing that you will calling this method from a thread other then UI Main Worker thread.
For debugging purpose try calling this method in the very beginining of app in the main class by main thread.
If it appears then make your threading issue clear in the app.
Use Handler class object to reslove such issues. I will put some more light if you need
The problem is that height1 is not found within the dialog. Really, you shouldn't seearch one within the dialog, because it belongs not to the dialog, but to the activity. Try this:
final EditText height1 = (EditText) YourActivityClassName.this.findViewById(R.id.h1);
It will search for the R.id.h1 within your activity.
Related
package com.example.riplee07.trydialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> listss = new ArrayList<String>();
ArrayAdapter <String>adapter;
String add1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tt = (EditText)findViewById(R.id.editText);
final ListView lv = (ListView)findViewById(R.id.listView1);
Button br = (Button)findViewById(R.id.button);
add1 = tt.getText().toString();
adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,listss);
lv.setAdapter(adapter);
listss.add(add1);
adapter.notifyDataSetChanged();
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View ss = LayoutInflater.from(MainActivity.this).inflate(R.layout.textt,null);
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainActivity.this);
builderDialog.setView(ss)
.setPositiveButton("Print", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listss.add(add1);
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancel", null)
.setCancelable(false);
AlertDialog alert = builderDialog.create();
alert.show();
}
});
}
}
Try this code below (for getting edittext data):
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View ss = LayoutInflater.from(MainActivity.this).inflate(R.layout.textt,null);
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainActivity.this);
builderDialog.setView(ss)
.setPositiveButton("Print", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText et = (EditText) ss.findViewById(E.id.editText);
listss.add(et.getText().toString());
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancel", null)
.setCancelable(false);
AlertDialog alert = builderDialog.create();
alert.show();
}
});
Here is the method in which i call on Button button.setOnClickListener
public void saveNote(View view){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.add_new_note_layout);
mTitle = (EditText)dialog.findViewById(R.id.titleID);
mMessage = (EditText)dialog.findViewById(R.id.messageID);
save = (Button)dialog.findViewById(R.id.saveBtn);
setFilePathll();
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int titleLength = mTitle.getText().length();
if(titleLength != 0){
String title = mTitle.getText().toString();
String msg = mMessage.getText().toString();
helper.addNote(title, mFilePath, msg);
dialog.dismiss();
}
else
{
Toast.makeText(MainActivity.this,"Empty Message is not valid",Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
Here helper.addNote(title, mFilePath, msg); is the method which i declared in the database helper class to insert data.
you Should use RecyclerView for what you want instead of using 'ListView'
The question is as you can see that I cant set the value of edittext dynamically before showDialog().
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int DIALOG_TEXT_ENTRY = 7;
private int user_id;
private int dialogChoice;
private String mobileNum;
private EditText input2 ;
private EditText input1 ;
public TextView textView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog_text_entry);
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mobileNum =tm.getLine1Number();
// input1.setText(mobileNum);
textView.setText("hello");
showDialog(DIALOG_TEXT_ENTRY);
}
#Override
protected Dialog onCreateDialog(int i) {
switch (i) {
case DIALOG_TEXT_ENTRY:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
input2 = (EditText) textEntryView.findViewById(R.id.number_edit);
input1 = (EditText) textEntryView.findViewById(R.id.username_edit);
textView = (TextView) textEntryView.findViewById(R.id.username_view);
return new AlertDialog.Builder(MainActivity.this)
// .setIconAttribute(android.R.attr.accountType)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent i = new Intent(MainActivity.this, IccActivity.class);
startActivity(i);
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}
return null;
}
}
textView.setText("hello"); this line kills the app.
thanks in advance.
you should put this line textView.setText("hello"); into onCreateDialog() method as you are setting the value before it gets initialized.
You haven't initialized textView. You need this in your onCreate() before you do the textView.setText()
textView = (TextView) textEntryView.findViewById(R.id.username_view);
I guess you are getting NullPointerException? You should first ser your 'textView' field.
Find the TexView text that is in the dialog and set the text of the TextView
Try this link
Here is my code:
package com.example.userpage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class UserPage extends Activity {
AlertDialog.Builder builder;
private final static int EMPTY_TEXT_ALERT = 0;
#Override
public Dialog onCreateDialog(int id) {
switch(id) {
case EMPTY_TEXT_ALERT: {
builder = new AlertDialog.Builder(this);
builder.setMessage("Message:Fields Empty!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
return null;
}
String tv,tv1;
EditText name,pass;
TextView x,y;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.widget44);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent obj = new Intent(UserPage.this,UserPage.class);
startActivity(obj);
}
});
x = (TextView) findViewById(R.id.widget46);
y = (TextView) findViewById(R.id.widget47);
name = (EditText)findViewById(R.id.widget41);
pass = (EditText)findViewById(R.id.widget43);
Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//tv= name.getText().toString();
//tv1 = pass.getText().toString();
x.setText(tv);
y.setText(tv1);
tv = name.getText().toString();
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
tv1 = pass.getText().toString();
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}
}
});
}
}
What is happening when you try to run it? Which part are you having a problem with?
From what I can tell your code is not going to display any dialogs because you've never called the dialog.show() method. You'd have to do something like this for the way you have it set up:
showDialog(EMPTY_TEXT_ALERT).show();
If you are trying to make it two separate dialogs, one for name, and one for pass then all you'd have to do is make another id variable and add a case: for it inside the switch statement that inside your showDialog(id) method.
You should also really consider using descriptive names for your variables. Your code would be easier to understand if you didn't use names like x,y, and widget#.
My code:
package com.example.linkingpage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LinkingPage extends Activity {
String tv,tv1;
EditText name,pass;
TextView x,y;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.widget44);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent obj = new Intent(LinkingPage.this,LinkingPage.class);
startActivity(obj);
}
});
x = (TextView) findViewById(R.id.widget46);
y = (TextView) findViewById(R.id.widget47);
name = (EditText)findViewById(R.id.widget41);
pass = (EditText)findViewById(R.id.widget43);
Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
x.setText(tv);
y.setText(tv1);
tv = name.getText().toString();
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
tv1 = pass.getText().toString();
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}
else
{Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name", name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
}
});
}
private final static int EMPTY_TEXT_ALERT = 0;
#Override
public Dialog onCreateDialog(int id) {
switch(id) {
case EMPTY_TEXT_ALERT: {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Message:Field Empty!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
return null;
}
}
When both fields are kept empty, and button is clicked alert is generated. Same happens when only name field is filled. But when name field is kept empty and passwor is filled , then though an alert is generated but link to other page is also generated showing my password.How can i prevent this??
Its because your your else that starts your next activity is only applying to your second if statement(the one for the password)
this:
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
is completely separate from this:
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}else
{
Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name",name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
In order to make it check both of them you should do something like this:
if(tv.trim().equals("") || tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT)
}else{
Intent obj = new Intent(LinkingPage.this,ResourcePage.class);
obj.putExtra("name",name.getText().toString());
obj.putExtra("pass", pass.getText().toString());
startActivity(obj);
}
And again I really cannot stress enough how much easier your could would be to understand if you used descriptive names for your variables instead of things like tv,tv1,button,button1,widget38 etc...
Remove the code from else and put it right there without else there is no requirement of else and after showDialog put return;
and dont post duplicate posts
Can someone explain to me why this AlertDialog crashes?
package com.clicker;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Clicker extends Activity
{
public int clickerNumber = 0;
private TextView clickerText;
private Button clickerButton;
private Button resetButton;
// Called when the activity is first created.
#SuppressWarnings("null")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Declare each of the layout objects
clickerText = (TextView)findViewById(R.id.clickerText);
clickerButton = (Button)findViewById(R.id.clickerButton);
resetButton = (Button)findViewById(R.id.resetButton);
clickerText.setText("0");
final AlertDialog.Builder resetQuestion = null;
resetQuestion.setTitle("Reset?");
resetQuestion.setMessage("Are you sure you want to reset the counter?");
resetQuestion.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
clickerNumber = 0;
clickerText.setText("0");
}
});
resetQuestion.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
clickerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clickerNumber++;
clickerText.setText(Integer.toString(clickerNumber));
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resetQuestion.show();
}
});
};
};
This is a great fail:
final AlertDialog.Builder resetQuestion = null;
resetQuestion.setTitle("Reset?");
You are trying to use a null object, and that (of course) will throw a NullPointerException
This is how I create dialogs (and I think it's the best way to do it):
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialogo_layout, null);
final AlertDialog.Builder resetQuestion = new AlertDialog.Builder(YourActivity.this)
// do whatever you want with the resetQuestion AlertDialog
Here, R.layout.dialogo_layout represent a file called dialogo_layout.xml in the res/layout dir that contains the dialog layout.