How to use new class wiith media player in main activity? - java

I'm trying to create new class, becouse I will have there many of sound file, but I don't know how to use it in main ativity. Here is my example code of sound.java:
package app.damian.komunikat_v1;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
public class sound extends MainActivity{
final MediaPlayer sound1 = MediaPlayer.create(this,R.raw.miau);
final MediaPlayer sound2 = MediaPlayer.create(this,R.raw.lol);
}
And now i'm trying to use sound1.start(); in main activity, but i don't know how. Any suggestions?
EDIT:
This is my MainActivity.java:
this is my MainActivity.java:
package app.damian.komunikat_v1;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button guzik = findViewById(R.id.button);
final MediaPlayer sound1 = MediaPlayer.create(this,R.raw.miau);
guzik.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sound1.start();
};
});
}
}

I found a different solution. Instead of creating new class with list of sound file, I had created an array of sounds in main activity class.

Related

the Android Studio OnClickListener doesn't work

Nothing happens when I click the "start" button, the app suddenly closes. I think the View.OnClickListener doesn't work, but I can't find how to fix it.
package com.example.myquiz;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView title;
private Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = findViewById(R.id.main_title);
start = findViewById(R.id.ma_startB);
Typeface typeface = ResourcesCompat.getFont(this,R.font.blacklist);
title.setTypeface(typeface);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
startActivity(intent);
}
});
}
}
I got this error in Logcat:
error screenshot
This is the code for the CategoryActivity class, this is where I get the error from, but I really don't see where the problem is.
package com.example.myquiz;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class CategoryActivity extends AppCompatActivity {
private GridView catGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Categories");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catGrid = findViewById(R.id.catGridView);
List<String> catList = new ArrayList<>();
catList.add("Cat 1");
catList.add("Cat 2");
catList.add("Cat 3");
catList.add("Cat 4");
catList.add("Cat 5");
catList.add("Cat 6");
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
{ CategoryActivity.this.finish();}
return super.onOptionsItemSelected(item);
}
}
The click listener is working correct, looking at the error log the launching of the CategoryActivity class is causing the app crash:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor...
This tells the ActionBar / ToolBar inside the CategoryActivity is setup twice. Probably (if not, please share the class code) you can either remove setSupportActionBar() or change the theme of the CategoryActivity to something like: Theme.MaterialComponents.Light.NoActionBar to see if you get the Activity running after the button click.

How to fix "error: cannot find symbol method startActivity(Intent)" in a class of Listener?

I can make an Intent to open other Activity with writing the code in MainActivity.java.
Then I try to make an Intent using a class and called it in MainActivity.java. But it becomes error.
How to solve this problem?
When I write startActivity(numberIntent); in MainActivity.java there is no error but when I move this line of code to NumbersClickListener.java
Errors come:
error: cannot find symbol method startActivity(Intent)
error: not an enclosing class: MainActivity
This my code
In MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
in NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
android.widget.Toast first
import android.widget.Toast;
OnClickListener should be written in capital letter
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {//.makeText(view.getContext(),
"open the list of numbers", Toast.LENGTH_SHORT).show();
Intent numberIntent = new Intent(MainActivity.this,
NumbersActivity.class);
startActivity(numberIntent);
}
}
error: cannot find symbol method startActivity(Intent)” in a class of Listener?
Because if startActivity(Intent) is a method of activity and its required call from context
If you want call startActivity(Intent) outside activity you need to use
Context.startActivity(numberIntent);
Use this
view.getContext().startActivity(numberIntent);
instead of this
startActivity(numberIntent);
SAMPLE CODE
public class NumbersClickListener implements View.OnClickListener {
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(view.getContext(),
NumbersActivity.class);
view.getContext().startActivity(numberIntent);
}
}
You are defining NumbersClickListener in a separate java file. It has no way compiler will know that when u call startActivity you are referring to the Activity.startActivity
Unless you have a deeper purpose for NumbersClickListener.java, simply do inline declaration of View.Listener will do
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener();
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
}
});
}
In place MainActivity.this, use its context.
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
Notice the changes I have made
MainActivity.java
package com.example.android.*****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumbersClickListener clickListener = new NumbersClickListener(MainActivity.this); // Context while creating ClickListener Object
TextView numbers = (TextView)findViewById(R.id.numbers);
numbers.setOnClickListener(clickListener);
}
NumbersClickListener.java
package com.example.android.*****;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
public class NumbersClickListener implements View.OnClickListener {
Context context;
NumbersClickListener(Context c){
this.context = c;
}
#Override
public void onClick(View view) {
Intent numberIntent = new Intent(context, NumbersActivity.class);
startActivity(numberIntent);
}
}
To startActivity you need Context.
It will be like this context.startActivity()
In MainActivity it is not giving error because Activity internally extends Context.
NumbersClickListener is not extended Context.
So, you can start activity using View context
Replace startActivity(numberIntent) with
view.getContext().startActivity(numberIntent);
inside your NumberClickListener class you can do the following
Context context = view.getContext();
Intent numberIntent = new Intent (context, NumberActivity.class);
context.startActivity(numberIntent);
By using this code you can use your NumberClickListener with any other activity.
happy codding :)

MediaPlayer.create cannot be resolved to a type

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

How can make a relation between 2 class in one program?(android)

I must write a program with android which can find other ssid's and show them. I desinged it with 2 xml pages. I create an imagebutton in page2 and want to make a relation between imagebutton and searching method. It means i want to click on imagebutton and seaching method begin it's work and search ssid's and show them...
My problem is, I download my search method and because of that i can not recognize which method i must call on my setonclick method that i write for an imagebutton in second page? I try to create another class seperately for search method and call it from the second class of page2.
but i dont know how can i make a relation between these 2 calss(i mean the second and third class). Or i must write the method of searching and on click of my imagebutton in one class?
Thanks for your suggestion.this is the code that i was copy:
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class wifiScan extends Activity {
private class WifiReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifi.getScanResults();
Date tempDate=new Date();
String info=testNumber+" "+(tempDate.getTime()-testDate.
getTime()) +" "+results.size();
Log.i("wifiScan", info);
wifiText.setText(info);
testNumber++;
testDate=new Date();
wifi.startScan();
}
}
private TextView wifiText;
private WifiManager wifi;
private WifiReceiver receiver;
private Date testDate;
private static int testNumber=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
testNumber=0;
wifiText = (TextView) findViewById(R.id.wifiText);
receiver=new WifiReceiver();
registerReceiver(receiver, new IntentFilte
(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){
wifi.setWifiEnabled(true);
}
startScan();
}
#Override
public void onStop(){
super.onStop();
finish();
}
public void startScan(){
testDate=new Date();
wifi.startScan();
}
}
you_image_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(this, wifiScan.class);
getApplicationContext().startActivity(i);
}
});

imageView android not recognized error

I am wondering why image view in java class pots error
like ImageView smtin;
And there's error.
It dont recognize it like class...
My project code is:
package com.klemenjezakon.koceSLO;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
ImageView slika;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent i = getIntent();
koca podatki=(koca)i.getSerializableExtra("koca");
setContentView(R.layout.koca);
}
}
You have to put inside your class. So instead of:
ImageView slika;
public class MainActivity extends Activity {
it have to be:
public class MainActivity extends Activity {
ImageView slika;

Categories