Media Player constructor - Android Q&A, Help & Troubleshooting

Hello guys, I've just start to write simple android apps and I get stuck with this problem. I have an activity with one button (startPlay) and I want to play a mp3 file (a1.mp3) from raw directory when the button is clicked. When I try to run the app on virtual device I get this error:
Error22, 25) error: constructor MediaPlayer in class MediaPlayer cannot be applied to given types;
required: no arguments
found: MainActivity,int
reason: actual and formal argument lists differ in length
Click to expand...
Click to collapse
Here is my code:
Code:
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.startPlay);
button.setOnClickListener(this);
}
public void onClick(View v) {
MediaPlayer m = new MediaPlayer(this,R.raw.a1);
m.start();
}
}

Related

[Q] Linking Strings between Activities and WebViews

Hello XDA,
So I've not found anyone properly discussing this. If they do, please link me to the page and don't flame
I've been making an app for my school and so far, I've been doing alright. But now I have run into a problem which seems to be very hard to solve for me alone.
Namely, I have an EditText and a Button in one Activity and a WebView as a different Activity.
I'm trying to get the EditText String into the WebView URL (String being variable, Button initiating the WebView), but get a lot of errors, some not even related to that.
I would really appreciate it if someone could have a look at the code and help me with this
MyWebView.java:
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebView extends Activity {
WebView mWebView;
EditText mEtxtStudentNum;
static String StudentNumFromHome = bas.sie.Antonius.Home.StudentNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://carmelcollegegouda.nl/site_ant/roosters/standaardroosters/Lee1_" + StudentNumFromHome + ".htm");
}
}
Home.java (Homescreen):
Code:
package bas.sie.Antonius;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Home extends Activity {
Button mBtnStSchedule;
static EditText mEtxtStudentNum;
static final String StudentNum = mEtxtStudentNum.getText().toString();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.BtnStSchedule);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MyWebView.class);
startActivityForResult(myIntent, 0);
}
});
// TODO Auto-generated method stub
}
}
AntoniusActivity.java (Main):
Code:
package bas.sie.Antonius;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class AntoniusActivity extends TabActivity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Home.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, External.class);
spec = tabHost.newTabSpec("external").setIndicator("External",
res.getDrawable(R.drawable.ic_tab_external))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Contact.class);
spec = tabHost.newTabSpec("contact").setIndicator("Contact",
res.getDrawable(R.drawable.ic_tab_contact))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
In AntoniusActivity, the private class HelloWebViewClient is underlined with yellow (never used), specifically the bit "HelloWebViewClient".
It's also throwing FC's at startup, and errors in LogCat, but I'll post those later on, as it's 10.30 PM here, and school goes on
Thanks in advance,
bassie1995
No suggestions yet?
How you doin'? Greetings from my GT-I9000!

Android App Help

Ok so today i started my first Android Application in eclipse. I am VERY new at coding so bear with me. So i looked online on how to open a new screen when i press a button, but my question is, how do i open a different screen when i press a different button on the same page? Because you cant just copy and paste the same code for the first button because it gives you duplicate errors. Can anyone tell me what i can copy/paste or do to get my second button to go to a different screen? Thanks!
main.class (or java)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MinecraftInfoProActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
}
}
main.xml
Code:
animals.class (or java) (this is my second page i wanna hook my second button up to)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Animals extends Activity {
}
animals.xml
Code:
AndroidManifest.xml
Code:
I am trying to hook button2 in main.xml so that when clicked, it takes me to the animals.xml page. Any help is appreciated guys. Thanks
EDIT: Sorry about the smily faces in the code..im not sure what that is about.
Mod Edit: Moved to Q&A
Scoutamis said:
Ok so today i started my first Android Application in eclipse. I am VERY new at coding so bear with me. So i looked online on how to open a new screen when i press a button, but my question is, how do i open a different screen when i press a different button on the same page? Because you cant just copy and paste the same code for the first button because it gives you duplicate errors. Can anyone tell me what i can copy/paste or do to get my second button to go to a different screen? Thanks!
main.class (or java)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MinecraftInfoProActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
}
}
main.xml
Code:
animals.class (or java) (this is my second page i wanna hook my second button up to)
Code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Animals extends Activity {
}
animals.xml
Code:
AndroidManifest.xml
Code:
I am trying to hook button2 in main.xml so that when clicked, it takes me to the animals.xml page. Any help is appreciated guys. Thanks
EDIT: Sorry about the smily faces in the code..im not sure what that is about.
Click to expand...
Click to collapse
Use CODE] /CODE] If you dont want smileys.
Sent from my GT-I5800 using XDA App
rubensollie said:
Use CODE] /CODE] If you dont want smileys.
Sent from my GT-I5800 using XDA App
Click to expand...
Click to collapse
I did so idk why it isn't working...anyway does ANYONE have any help?
Sent from my SAMSUNG-SGH-I727 using xda premium
Hope this helps ya out
In the main.xml, you can specify an event handler method for button2 by adding this in button2's xml declarations
Code:
android:onClick="yourMethodName"
That is a quick replacement for
Code:
button.setOnClickListener(new OnClickListener(){
public void yourMethodName(View view) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
Of course replace 'yourMethodName' with the method that you want to handle the click action. You can use this for button1 as well, just specify the onClick property in main.xml under button1. You need to create a method in your MinecraftInfoProActivity class that will be the handler for the button, so say you create a method called 'Button2Handler". To open up Animals (which you have to write the code to display the animals.xml) you could use this method.
Code:
public void Button2Handler(View view) {
Intent intent = new Intent(context, Animals.class);
startActivity(intent);
}
Hope this gives you what you need, as it's just a quick basic intro into it
regaw_leinad said:
Hope this helps ya out
In the main.xml, you can specify an event handler method for button2 by adding this in button2's xml declarations
Code:
android:onClick="yourMethodName"
That is a quick replacement for
Code:
button.setOnClickListener(new OnClickListener(){
public void yourMethodName(View view) {
Intent intent = new Intent(context, Mobs.class);
startActivity(intent);
}
});
Of course replace 'yourMethodName' with the method that you want to handle the click action. You can use this for button1 as well, just specify the onClick property in main.xml under button1. You need to create a method in your MinecraftInfoProActivity class that will be the handler for the button, so say you create a method called 'Button2Handler". To open up Animals (which you have to write the code to display the animals.xml) you could use this method.
Code:
public void Button2Handler(View view) {
Intent intent = new Intent(context, Animals.class);
startActivity(intent);
}
Hope this gives you what you need, as it's just a quick basic intro into it
Click to expand...
Click to collapse
Omg thank you that helped soooo much
Sent from my SAMSUNG-SGH-I727 using xda premium

[Q]ERROR: Void is an invalid type for the variable

I just started developing apps and I've come across the error: Void is an invalid type for the variable button1Click. I got the error on the following line of code
public void button1Click; {
It's in my java class, I'm trying to open an activity from another. This is my entire code looks like this
package trial.play;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class Food extends Activity implements View.OnClickListener {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singer);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
public void button1Click; {
startActivity(new Intent("android.intent.action.TESTMENUACTIVITY"));}
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.button1:
button1Click: break;
}
}
public void Enter(View view) {
Intent openTestMenuActivity = (new Intent(
"trial.play.TESTMENUACTIVITY"));
startActivity(openTestMenuActivity);
}
}
I'm still new to development so be specific and show me what you mean if you can.
Thanks

[Q] Runtime.getRuntime().exec dosn't work

Hi i'm trying to make an app with a button that free cache of the phone.. This is the code:
Code:
package com.mkyong.android;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import com.example.toast.R;
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@SuppressLint("SdCardPath")
@Override
public void onClick(View arg0) {
final Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("su -c 'echo 3 > /proc/sys/vm/drop_caches'");
Toast.makeText(MainActivity.this, "Script lanciato con `successo, memoria svuotata.", Toast.LENGTH_LONG).show();`
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Well i've got no errors but nothing happen.. When i press the button the free ram memory rest the same. (to see the free memory i open the terminal emulator and i write the "free" command"). Someone can help me? What's wrong?

[Q] Some problem with my code

Hi, I having some problem with this simple code. I don't undestrand because ADT impost fields as EditText, TextView and Button as field final. I think that there' some errore in my code, I admit that is one of my first times that i programming for andoird, so have patience with me.
I apologize if I wrong section :silly:
That's my code:
package com.example.buttoncerca;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RisponditoreInterattivoActivity extends Activity implements Lista {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_risponditore_interattivo);
final EditText text = (EditText)this.findViewById(R.id.campoNome);
Button button = (Button)this.findViewById(R.id.buttone);
final TextView tv = (TextView)this.findViewById(R.id.testoSaluto);
button.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Editable nome = text.getText();
String s = nome.toString();
for(int c =0; c<3; c++){
if(toppings[c].equals(s)){
tv.setText(toppings[c] + " si butta nel " + cestino[c]);
} //End if
}//End for
}//End onClick
});
}
}
Click to expand...
Click to collapse
I implements inteface Lista, this is code:
package com.example.buttoncerca;
public interface Lista {
String[] toppings = {"Plastica", "Legno", "Cartone"};
String[] cestino = {"Cestino plastica", "Cestino legno", "Cestino cartone"};
}
Click to expand...
Click to collapse
And... what's the problem? Please post it "as is".
You need to instantiate the editable first, with BUFFERTYPE.EDITABLE, because in your current case you are casting a charseq to an editable so that's likely the exception you are getting (hard to know without a logcat). See here:
http://developer.android.com/reference/android/widget/EditText.html#getText()
Sent from my Amaze 4G using xda app-developers app

Categories

Resources