I'm working on a simple number guessing game. At the moment I have it set that when you guess the correct number it takes you to WinActivity.java. All I want it to do on that screen is say "You won" and to play a little trumpet victory sound (and also display a button that'll bring them to the GameActivity if they want to play again).
The trouble is, when I try bringing in a MediaPlayer the app doesn't even start.
Here is my code for my WinActivity that contains the MediaPlayer.
package com.example.jeremy.numberguessinggame;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WinActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_win);
MediaPlayer trumpetsound = MediaPlayer.create(this,R.raw.trumpet);
trumpetsound.start();
Button btnPlayAgain = (Button) findViewById(R.id.button2);
TextView youWon = (TextView) findViewById(R.id.textViewYouWon);
btnPlayAgain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent playAgainIntent = new Intent(WinActivity.this,GameActivity.class);
startActivity(playAgainIntent);
}
});
}
}
When I run the app the Messages Gradle Build window will pop up and says "error: cannot find symbol variable raw".
I added the raw folder to the res location, and was able to drag the sounds into the folder no problem.
I just don't see why I can't make this MediaPlayer work.
I would love some help. If you need more information I'll be glad to give it to you.
Related
I have tried to install OpenCV for Java-App Development in Android Studio. The app starts on the emulator and tried to load but instantly shuts down. This happens when the app encounter anything related to OpenCV in the code (excluding imports).
This is not the entire project, if you want to compile and need everything drop a comment
package com.example.cse535a1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.content.Context;
import android.view.View;
import android.hardware.*;
import android.widget.FrameLayout;
import org.opencv.core.*;
public class MainActivity extends AppCompatActivity {
Camera c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))) {
this.finish();
System.exit(0);
}
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME); ---------------------------------CRASHES HERE
// Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
// Log.i("OPENCV", mat.dump());
Button button_symptoms = (Button)findViewById(R.id.button_symptoms);
Button button_upload_signs = (Button)findViewById(R.id.button_upload_signs);
Button button_measure_heart_rate = (Button)findViewById(R.id.button_measure_heart_rate);
Button button_measure_respiratory_rate = (Button)findViewById(R.id.button_measure_respiratory_rate);
c = getcam();
CameraView cv1 = new CameraView(getApplicationContext(), c);
FrameLayout view_camera = (FrameLayout)findViewById(R.id.view_camera);
view_camera.addView(cv1);
button_symptoms.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
Intent intent = new Intent(getApplicationContext(), Loggin_symptoms.class);
startActivity(intent);
}
});
button_upload_signs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_heart_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
}
});
button_measure_respiratory_rate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg_view) {
SensorManager manager_sensor = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor_accelerometer = manager_sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
});
}
I have previously done some research before posting on StackOverflow and I found this->
Android crashes when using OpenCV from activity
This has led me to the install instructions on-> Installing OpenCV 2.4.13.7. The necessary part can be found under Application Development with Static Initialization.
But unfortunately, the entire process of having an Android.mk file is no longer in use. So to solve this problem what do I do now?
Is there a different file where the OPENCV_INSTALL_MODULES:=on line must be inserted?
So, I think I have found the solution to this problem in my case.
Upon looking in the Logcat tab in Android studio, I noticed a problem that libopencv_java451.so was not found. In the jniLibs folder there was instead libopencv_java4.so.
A simple rename fixed this problem. There are two places where I renamed the files.
jniLibs folder in the project directory
jniLibs in the opencv sdk directory (though this one might be ultimately responsible)
Depending on the system (for me-> x86 and x86_64), rename your files.
I have attached a pic showing my project file structure, look at the large yellow marks which show the renamed files.
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'm trying to get an mp3 file to play a sound when a button is clicked (Android app). However, despite following tutorials with similar code, I get this error message 'MediaPlayer.create cannot be resolved as a type. I have refreshed the project and cleaned the project and it made no difference. Here is my code:
package com.example.h.a.t;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Hypo_Info extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hypo_info);
final MediaPlayer mediaPlayer = new MediaPlayer.create(this, R.raw.pill_bottle);
Button a = (Button) findViewById(R.id.treatment_hypo);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Hypo_Info.this, Hypo_Treatment.class);
startActivity(i);
mediaPlayer.start();
}
});
}
The problem is the "new" keyword here:
new MediaPlayer.create(this, R.raw.pill_bottle);
because the MediaPlayer.create is a static method.
==> remove the new
So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes
Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project