I have problems with opening an activity, I know that I have to implement interfaces to these interfaces of OnClickListener, OnInitListener. I have tried with #Override. In my android studio it says that I have to implement an abstract on onclick() method in onclicklistener. I am a newbie in programing so any help is much appreciated!
public void addGlossary(View v){
Intent intent = new Intent (this, addGlossary.class);
Button buttonZero = (Button) findViewById(R.id.buttonZero);
startActivity(intent);
}
Here is the code for the activity:
public class addGlossary extends Activity implements OnClickListener, OnInitListener {
private int MY_DATA_CHECK_CODE = 0;
private TextToSpeech myTTS;
private MediaPlayer mMediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_glossary);
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(this, R.raw.button);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.stop();
}
});
When I have implemented those interfaces, and I try to connect to the activity my phone says that the application has stopped. I do not know what the problem is. Any help is appreciated.
What I implemented: onInit was already done
#Override
public void onClick(View v){
Button speakButton = (Button)findViewById(R.id.speak);
speakButton.setOnClickListener(this);
}
You must implement all the abstract methods in addGlossary , add following code to the class,
1) onClick for OnClickListener
#Override
public void onClick (View v) {
//do someting when the view clicked
}
2) onInit for OnInitListener
#Override
public void onInit (int status) {
//do someting when tts onInit
}
Click here to learn more about Java Interface.
Related
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
ListView listView;
String name;
private static final int REQUEST_CODE = 1;
ArrayAdapter<String> adapter;
ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
listView = findViewById(R.id.CarList);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
});
nameList = new ArrayList<String>();
nameList.addAll(Arrays.asList(name));
adapter = new ArrayAdapter<String>(this, R.layout.element, nameList);
listView.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent i){
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
name = i.getStringExtra("name");
}
}
}
And this is my SecondActivity:
public class SecondActivity extends AppCompatActivity {
EditText editText;
Button button2;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
button2 = findViewById(R.id.button);
}
public void finish() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView.
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
This is simply because you've incorrectly thinking that Activity.finish() is always called whenever you close the activity. But it never be called by the Android. Take a look for this Lifecyle picture from Activity-lifecycle concepts:
you can see that only onStop() then onDestroy() is called when activity is closed.
You need to call the finish() method manually to send your intent. Or, the better way, create a method that only build and set the intent for the result. Something like this:
private void prepareResult(String name) {
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
}
then call it whenever you want to close your activity:
String name = editText.getText().toString();
prepareResult(name);
finish();
Or you can override the onBackPressed() to also handing the back pressed, like the following:
#Override
public void onBackPressed() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
// The default implementation simply finishes the current activity
// see the documentation.
super.onBackPressed();
}
So I'm trying to create an intent which would play current streaming video in an external player but I'm stuck with this setOnItemClickListener being red and can't fix it.
Here's my code:
public class DetailsActivity extends AppCompatActivity {
public static ImageView imgBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
imgBack=findViewById(R.id.img_back);
imgBack.setOnItemClickListener(new ServerApater.OnItemClickListener() {
#Override
public void onItemClick(View view, CommonModels obj, int position, ServerApater.OriginalViewHolder holder) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(obj.getStremURL()), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
});
}
And please don't call it duplicate of a few similar questions since I looked up everywhere and can't get the answer :(
You are calling a wrong listener, you need OnClickListener instead of OnItemClickListener, it should be like this:
imgBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(obj.getStremURL()), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
});
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
I am new in android development and do not have an in depth knowledge of Java. I am stuck on a problem for a long time. I am trying to open a new activity on button click. But I am getting an error that error: not an enclosing class: Katra_home.
Here is the code for MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.bhawan1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Katra_home.this, Katra_home.class);
Katra_home.this.startActivity(myIntent);
}
});
And this is the code for Katra_home.java
public class Katra_home extends BaseActivity {
protected static final float MAX_TEXT_SCALE_DELTA = 0.3f;
private ViewPager mPager;
private NavigationAdapter mPagerAdapter;
private SlidingTabLayout mSlidingTabLayout;
private int mFlexibleSpaceHeight;
private int mTabHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.katra_home);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
Though I have seen many answers on stackoverflow but I could not understand them as I am new in android development. So I would like to ask what changes do I need to make in my code to make it work.
It should be
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
You have to use existing activity context to start new activity, new activity is not created yet, and you cannot use its context or call methods upon it.
not an enclosing class error is thrown because of your usage of this keyword. this is a reference to the current object — the object whose method or constructor is being called. With this you can only refer to any member of the current object from within an instance method or a constructor.
Katra_home.this is invalid construct
Intent myIntent = new Intent(MainActivity.this, Katra_home.class);
startActivity(myIntent);
This Should the perfect one :)
you are calling the context of not existing activity...so just replace your code in onClick(View v) as
Intent intent=new Intent(MainActivity.this,Katra_home.class);
startActivity(intent);
it will definitely works....
String user_email = email.getText().toString().trim();
firebaseAuth
.createUserWithEmailAndPassword(user_email,user_password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(RegistraionActivity.this, "Registration sucessful", Toast.LENGTH_SHORT).show();
startActivities(new Intent(RegistraionActivity.this,MainActivity.class));
}else{
Toast.makeText(RegistraionActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
}
});
replace code in onClick() method with this:
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
startActivity(new Intent(this, Katra_home.class));
try this one it will be work
So I have this code in my main activity to start a new one:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.GoButton).setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(MainActivity.this, NewActivity.class);
MainActivity.this.startActivity(myIntent);
//finish();
}
});
}
}
My new activity extends ListActivity and when I call this code by pressing the button it crashes the application. However if I make the MainActivity extend ListActivity then it works great (although I have to replace the button with a List!). Does anyone know why this happens, and how can I make it work using the code above?
Thanks
Have you added the manifest entry