I followed the example of a Android book to draft a project.
However, there are some problems coming out:
When I touch a selection, the program will exit the program and returning a message "the application has stopped unexpectedly. Please try again"
Would anyone can help me?
here is the source code:
package com.example.ch07_dialogask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity //should be protected
implements DialogInterface.OnClickListener{
TextView txv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView)findViewById(R.id.answer);
new AlertDialog.Builder(this)
.setMessage("Do you like Android phone?")
.setCancelable(false)
.setIcon(android.R.drawable.ic_menu_edit)
.setTitle("Android survey")
.setPositiveButton("Like",this)
.setNegativeButton("Unlike",this)
.setNeutralButton("No Comment",null)
.show();
}
#Override
public void onClick(DialogInterface dialog, int id){
if(id==DialogInterface.BUTTON_POSITIVE){
txv.setText("You Like Android Phone!");
}
else if(id==DialogInterface.BUTTON_NEGATIVE){
txv.setText("You Dislike Android Phone.....");
}
}
Sorry, I can't comment yet. I don't have 50 reputation points yet.
Anyway, try putting the "new AlertDialog.Builder..." snippet on the onResume method of the activity.
I would bet that this is happening because the onCreate method isn't showing the layout yet. You can learn more about the Activity life cycle here: Android Developer: Activity. Also, try posting the Logcat with your questions. It helps a lot to learn what is going on.
Hope this helps you!
EDIT:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv = (TextView)findViewById(R.id.answer);
}
#Override
public void onStart(){ //Called when the activity is becoming visible to the user.
super.onStart();
new AlertDialog.Builder(this)
.setMessage("Do you like Android phone?")
.setCancelable(false)
.setIcon(android.R.drawable.ic_menu_edit)
.setTitle("Android survey")
.setPositiveButton("Like",this)
.setNegativeButton("Unlike",this)
.setNeutralButton("No Comment",null)
.show();
}
Related
I'm trying to develop a game app. At the gameover screen, I want to have a button that goes back to the start when you click it. But the problem is, it doesn't work. I really don't know why it doesn't work, I have tried everything but can't find the problem. Can someone help me?
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class GameOver extends AppCompatActivity {
MediaPlayer gameoversound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
Button weiter_button = (Button) findViewById(R.id.weiter);
weiter_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { goToMainActivity(); }
});
}
private void goToMainActivity() {
Intent back = new Intent( this, MainActivity.class);
startActivity(back);
}
}
Change this to GameOver.this
You don't have to cast widgets explicitly anymore, except for some special cases. It has been this way for a while.
If the game is over, you'd best insert a finish(); after launching the main Activity. You don't want users to be able to go back to the gameover screen by pressing the back button.
Set up FLAGS for your Intents. This comes in handy because if you didn't finish(); some Activity it remains in the stack, so it will be launching that one, but then you might want a new one. This will cause issues in navigation.
Add/ check your onBackPressed() methods for the 2 Activities.
Furthermore, specify that you are overriding the method
#Override
public void onClick(View view)
{
}
In the XML, <Button> tag, add
android:clickable=true
android:focusable=true
So I just started to learn Android Studio for android development and started going through the myFirstApp tutorial on their website. I am trying to add a method to the button but can't get it to work. I have the sendMessage method in MainActivity.java and when i go to select it from the "on click" drop down list, it doesn't appear. I have the correct imports as well. Does anyone know why this may be? Thanks.
Here is what my code looks like:
package com.example.tyler.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
Add android:onClick="sendMessage" attribute to your button tag in activity_main.xml.
From the tutorial you are following:
Now return to the activity_main.xml file to call this method from the button:
Click to select the button in the Layout Editor.
In the Properties window, locate the onClick property and select sendMessage [MainActivity] from the drop-down list.
You must've skipped this step.
I got stuck with this one as well, so i skipped down the page a bit and grabbed the complete code for MainActivity.java (apart from the first line that i kept).
Once i'd replaced this i got the sendMessage[MainActivity] appear on the onClick Button attribute.
package com.example.your....
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
The answer for this is that you do not have the function INSIDE of the last curly brace at the bottom. Just undo the function you pasted and make sure to repaste just before the last curly brace and then it will all be fixed.
Hi I have been racking my brain for hours, did research online but nobody seems to have an answer. My emulator was running my code no problem then I ran it again and I get "Session 'MainActivity': error". I looked through this main activity about 10 times but there is no error sign anywhere and it looks like it should be working fine, I mean it was working before no problem. So I'm not sure if there really is a problem I don't see that Android Studio is not pointing out properly or if this is a different problem all together.
Any help would be greatly appreciated. Thank you.
package tekvision.codedecrypter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private final Context context = this;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ans = gDB.getAnswer("Ancient",1);
//Toast pop up message
Toast toast = Toast.makeText(getApplicationContext(),
ans ,
Toast.LENGTH_SHORT);
toast.show();
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Try rebuilding and cleaning your project.
Build > Rebuild Project
and
Build > Clean Project
I built a web app, and I am making a wrapper to put it on the app store. I have a main page, or start page for the app, and the submit button successfully loads my webview page, and the app works well.
I wanted to add a small webview above the submit button where I could display updated news before users enter the app, such as new terms of use.
I followed examples to get the webview to load on the main page, and it works - the content is displayed, but when I add the webview, the submit button doesn't work any longer.
Eclipse throws an error "Duplicate Method OnCreate(Bundle) in type TOS, so I tried changing OnCreate to OnStart or OnCreateView for the button. It renders from the layout xml, but there is no functionality. I'm certain it is a novice syntax error, so I've just posted the Tos.java code:
package com.packagename.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tos extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
WebView TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl(someURL");
};
};
So you need only one onCreate method for each Activity not object, which is where you're thinking(I assume) that you need multiple onCreate methods.
So.. just merge the two you have...
public class Tos extends Activity {
//UI ELEMENTS
private Button button;
private WebView TermswebView;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tos);
TermswebView = (WebView) findViewById(R.id.webView);
TermswebView.loadUrl("http://www.barglance.com/assets/tos/tos.php");
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Mapp.class);
startActivity(intent);
}
});
}
I hope this helps. For more information about activities and what onCreate actually does and when it's called, refer to Android Docs - Activity
I am trying to implement facebook like slider, and i am planning to user below opensource code
https://github.com/leonardosalles/shipp-sliding-menu
but after i integrate, i created a sample activity
as per the code guide, in the
PrincipalActivity.java
//intent = new Intent(this, MyNewActivity2.class);
if it try giving my class, i get below error
cannot find symbol constructor Intent(,java.lang.Class)
here is my activity class
import android.app.Activity;
import android.os.Bundle;
public class SampleFirstTest extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
please Help me to resolve the issue, i am new to android..
add SampleFirstTest in your AndroidManifest.xml