App stopped working android - android

I don't understand why my "App" has stopped working?
This is my MainActivity.class code:-
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}
and my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.nishant.iwillguessyournumber.MainActivity">
<Button
android:id="#+id/cmdPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="219dp" />
<Button
android:id="#+id/cmdExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="328dp" />
</android.support.constraint.ConstraintLayout>
Whenever I am running this app on Genymotion Android:6.0.0 and API 23, it is showing "Unfortunately, (app name) is not working"
What is the problem in my code/platform?
I've tried restarting my apps, looked up a few questions but couldn't yet get my answer when I open a new Activity using Intent, the second activity can't parse the xml file.

The source of your app crash is the following 2 lines,
private Button btnplay = (Button)findViewById(R.id.cmdPlay);
private Button btnexit = (Button)findViewById(R.id.cmdExit);
The method findViewById() should be called inside the onCreate() after the setContentView(). The following code should work,
package com.apps.nishant.iwillguessyournumber;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnplay;
private Button btnexit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnplay = (Button)findViewById(R.id.cmdPlay);
btnexit = (Button)findViewById(R.id.cmdExit);
OnButtonClickListener();
}
public void OnButtonClickListener() {
btnplay.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
);
btnexit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Do you really wanna exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
}
);
}
}

Related

implementing a button inside a popup window for android

bellow you can see a script that displays the data from a SQLite database in a popup window. My question is that how can I implement a button inside that window to accomplish some tasks
here is the code:
package com.example.asus.sqlliteproject;
import android.app.AlertDialog;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DataBaseHelper myDB;
EditText Name,LastName,Grades;
Button AddData,ViewData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new DataBaseHelper(this);
Name = (EditText)findViewById(R.id.name);
LastName = (EditText)findViewById(R.id.lastName);
Grades = (EditText)findViewById(R.id.Grades);
AddData = (Button)findViewById(R.id.addDataButton);
ViewData = (Button)findViewById(R.id.view);
addData();
}
public void addData () {
AddData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean inserted = myDB.insertData(Name.getText().toString(),
LastName.getText().toString(),
Grades.getText().toString());
if (inserted) {
Toast.makeText(MainActivity.this, "Text Inserted!", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "Unsuccessful!", Toast.LENGTH_SHORT).show();
}
});
}
public void viewAll () {
ViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor res = myDB.getAllData();
if (res.getCount() ==0){
// show some message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("ID :"+ res.getString(0)+"\n");
buffer.append("Name :"+ res.getString(1)+"\n");
buffer.append("LastName :"+ res.getString(2)+"\n");
buffer.append("Grade :"+ res.getString(3)+"\n\n");
}
// show all data here
showMessage("Data",buffer.toString());
}
});
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
and here is a screen shot of the app and what I want to implement:
Click here for image
try this,
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Your code
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Your code
}
});
http://developer.android.com/guide/topics/ui/dialogs.html
builder.setPositiveButton("buttontext", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do some shit
}
it's simple.
public void showMessageDialog(String title , String Message)
{
AlertDialog dialog = new AlertDialog.Builder(Main3Activity.this)
.setTitle(title)
.setMessage(Message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//add your code here...
}
})
.show();
}
If you want to imlement your custom button - in left corner with custom properties, you have to use "custom layout" for AlertBox.
Something like this:
View tmLayout = View.inflate(ctx, R.layout.myalert, null);
TextView header= (TextView) tmLayout.findViewById(R.id.header);
Button btn= (Button) tmLayout.findViewById(R.id.btn);
btntzsetcur.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//callback
}
});
header.setText("header);
btn.setText("btn");
alerboxTimeZone = new AlertDialog.Builder(ctx).create();
alerboxTimeZone.setView(tmLayout);
Of course, you have to use XML file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/header"
style="#style/whitetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/empty" />
<LinearLayout
android:id="#+id/wrap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/empty"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
You can make a custom dialog with a custom view.
Dialog dialog = new Dialog(_context);
dialog.setContentView(YOUR_CUSTOM_VIEW); // put your xml file instead of YOUR_CUSTOM_VIEW
put buttons or whatever you need in your xml file and reach your buttons like this :
Button button = dialog.findViewById(BUTTON_ID); // R.id.YOUR_BUTTON
and then you can set onClickListener and whatever stuff you want for the component

Android : Evaluate Dialog

I want to evaluate a Dialog under Android.
My problem is that the Dialog does not wait
with returning a value until I pressed a
button. And when I use a Semaphore the
program hangs at Semaphore.acquire().
Why does it hang at Semaphore.acquire()?
Can you see where I go wrong?
Here is the main activity
package com.example.modaldialog;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDialog meinDialog = new mDialog(this);
if (meinDialog.ShowMyModalDialog() == 1)
Toast.makeText(this, "Pressed Button 1",
Toast.LENGTH_LONG).show();
}
}
and here the dialog class
package com.example.modaldialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;
import java.util.concurrent.Semaphore;
import android.app.Activity;
import java.lang.Runnable;
public class mDialog
{
int pressedButtonID;
Activity act;
private final Semaphore dialogSemaphore;
mDialog(Activity act_in)
{
act = act_in;
dialogSemaphore = new Semaphore(0, true);
};
final Runnable mMyDialog = new Runnable()
{
public void run()
{
AlertDialog errorDialog = new
AlertDialog.Builder(act).create();
errorDialog.setMessage("Press a Button!");
errorDialog.setButton("Button2", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 2;
dialogSemaphore.release();
}
});
errorDialog.setButton2("Button1", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 1;
dialogSemaphore.release();
}
});
errorDialog.setCancelable(false);
errorDialog.show();
}
};
public int ShowMyModalDialog() //should be called from non-UI thread
{
pressedButtonID = 0;
act.runOnUiThread(mMyDialog);
try
{
dialogSemaphore.acquire();
}
catch (InterruptedException e)
{
}
return(pressedButtonID);
}
}
I am not much aware about the Semaphore but you can achieve the same with Interface. I have updated your code please find it below,
package com.example.modaldialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;
public class mDialog
{
int pressedButtonID=0;
private HandleDialogEvent dialogEvent;
mDialog(HandleDialogEvent dialogEvent)
{
this.dialogEvent = dialogEvent;
}
public void ShowMyModalDialog() //should be called from non-UI thread
{
pressedButtonID = 0;
AlertDialog errorDialog = new
AlertDialog.Builder(act).create();
errorDialog.setMessage("Press a Button!");
errorDialog.setButton("Button2", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 2;
dialogEvent.getDialogId(pressedButtonID);
}
});
errorDialog.setButton2("Button1", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
pressedButtonID = 1;
dialogEvent.getDialogId(pressedButtonID);
}
});
errorDialog.setCancelable(false);
errorDialog.show();
}
public interface HandleDialogEvent{
public void getDialogId(int Id);
}
}

how to use alertdialog

package com.progme.wallkon;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class NextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ImageView im1;
im1 = (ImageView)findViewById(R.id.a_01_b);
im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im2;
im2 = (ImageView)findViewById(R.id.a_02_b);
im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
ImageView im3;
im3 = (ImageView)findViewById(R.id.a_03_b);
im3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Gmelon");
builder.setMessage("setting?");
builder.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
});
builder.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
});
return builder.create();
}
}
I wrote code in activity.java like this..
I want to use dialog in im1, im2, im3, and each have to get another event.
Then, I have to write 3 dialog?
and how I can set [//TODO Auto...] here that I use is like..
first dialog for im1,
second dialog for im2,
third dialog for im3..
Please help..
You can write a private variable for the alert dialog and reuse it, but not at the same time
private AlertDialog mDialog = new AlertDialog.Builder(this)
.setTitle("Gmelon")
.setMessage("setting?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag" , "Click YES");
}
})
.setNegativeButton("NO",
new android.content.DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("MyTag", "Click NO");
}
}).create();
now you can show the dialog where ever you want in your code.
It looks like you could just use showDialog(x) to me unless there is more to this question.

Android, what can i do with that listener i click on the button and doesnt responce

i don't know what is wrong with that code ..the emulator works but the button does not work with the listener.i did everything i cleaned the project and also i made a listener and it supposed to work.
package com.example.dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
public class MainActivity extends Activity {
CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
boolean[] checkedItems = new boolean[items.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button btn= (Button)findViewById(R.id.dialog);
// btn.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
showDialog(0);
}
protected Dialog onCreatDialog(int id) {
switch (id) {
case 0 :
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("this is a List of items..");
builder.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "Ok clicked",
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel cliked",
Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
getBaseContext(),
items[which]
+ (isChecked ? " checked"
: " unchecked"),
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
return null;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="#string/click_me" />
</RelativeLayout>
uncomment this code:
Button btn = (Button)findViewById(R.id.dialog);
btn.setOnClickListener(new View.onClickListener(){
#Override
public void onClick(View v) {
//Do your stuff
}
});
If you want to use your Activity as onClickListener, keep "this" on setOnClickListener, but your activity must implements OnClickListener interface, and implements its methods to work.

AlertDialog crashes application

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.

Categories

Resources