activity error in manifest file in android studio - java

I have a manifest error. It was fine at first time.
But i chagned it to "public class color_dia extends Dialog implements View.OnClickListener"
and it was red line on color_dia in manifest file.
when i click a button related to color_dia. it makes error
"java.lang.RuntimeException: Unable to instantiate activity ComponentInfo"
what should i do?
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import static android.graphics.Color.rgb;
public class color_dia extends Dialog implements View.OnClickListener {
private MainActivity mActivity;
public color_dia(Context context) {
super(context);
mActivity = (MainActivity) context;
}
public color_dia(Context context, int themeResId) {
super(context, themeResId);
mActivity = (MainActivity) context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Pick Line Color");
setContentView(R.layout.color_dia);
}
}

You cannot launch a Dialog using startActivity() or startActivityForResult(). You can only launch activities (class that extends Activity). A Dialog is shown in an Activity by calling showDialog() (or use DialogFragment). Please read about the difference between Dialog and Activity in the Android documentation or find a suitable tutorial to follow.

Related

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 :)

What's wrong with my code, please help me [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
MainActivity.java:
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 {
int currentPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyListener myListener = new MyListener();
Button startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(myListener);
Button pauseButton = (Button) findViewById(R.id.pause);
pauseButton.setOnClickListener(myListener);
}
}
MyListener.java:
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MyListener extends AppCompatActivity implements View.OnClickListener{
MediaPlayer musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_listener);
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.start:
musicPlayer.start();
break;
case R.id.pause:
musicPlayer.pause();
break;
}
}
}
Logcat shows the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.musicplayer/com.example.android.musicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
I am trying to create an app that plays audio from my res/raw file, but there seems to be something wrong with my code as it is not working, please help me.
Firstly, your MediaPlayer instance should reside within MainActivity, not MyListener, and MyListener should not extend an activity. In fact, you should move all of your code from MyListener into MainActivity, I don't really see a purpose for it in the snippet you've provided.
Secondly, You're creating your MediaPlayer outside of the Activity Lifecycle, while still trying to pass a Context to it:
public class MainActivity extends AppCompatActivity {
MediaPlayer musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
...
}
The activity has no Context (this) until the Activity Lifecycle has started, the way you've written it above is equivalent to defining musicPlayer in a constructor:
public class MainActivity extends AppCompatActivity {
MediaPlayer musicPlayer;
public MyListener() {
musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
}
...
}
This will also fail as the Activity Lifecycle has not yet started. What you need to do is declare musicPlayer as a member of the class, and then create an instance in onCreate() where the Context will have been initialsed:
public class MainActivity extends AppCompatActivity {
MediaPlayer musicPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_listener);
musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
}
...
}
To address your comment, here's an example of how it could all fit in to your MainActivity using lambdas:
public class MainActivity extends AppCompatActivity {
int currentPosition;
MediaPlayer musicPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicPlayer = MediaPlayer.create(this, R.raw.sound_file);
Button startButton = (Button) findViewById(R.id.start);
startButton.setOnClickListener(view -> musicPlayer.start());
Button pauseButton = (Button) findViewById(R.id.pause);
pauseButton.setOnClickListener(view -> musicPlayer.pause());
}
}

Android: Overriding onCreate method of main activity

I have a Java activity like so.
package com.xxx.yyy.overrideoncreate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Original oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I wish to split some of the onCreate instructions to a separate class, so I created this too.
package com.xxx.yyy.overrideoncreate;
import android.os.Bundle;
import android.util.Log;
/**
* Created by oox on 26/6/17.
*/
public class SubClass extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Overridden");
super.onCreate(savedInstanceState);
}
}
I intend for subClass.onCreate to override MainActivity.onCreate -- both Overridden and original oncreate messages should be displayed. However, that did not seem to happen, the overridden message did not appear in the Logcat.
Any idea what I did wrong?
Thanks in advance.
In order for SubClass's onCreate to be called, an instance of SubClass should be created instead of an instance of MainActivity when the application is launched.
For that to happen, SubClass should become the actual main activity of your application (i.e. the activity class registered in the AndroidManifest.xml which gets launched when the application is launched).

java.lang.ExceptionInIntilizerError in main

My app crashes straight away with an exception
Here is the code of my main activity
package com.example.orientation;
import android.os.Bundle;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity implements myList.Communication {
myList listFragment;
Details detailsFragment;
FragmentManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=getFragmentManager();
listFragment = (myList) manager.findFragmentById(R.id.listFragment);
listFragment.setCommunicator(this);
}
#Override
public void respond(int index) {
detailsFragment = (Details) manager.findFragmentById(R.id.detailFragment);
if (detailsFragment!=null && detailsFragment.isVisible()) {
detailsFragment.changeData(index);
}else{
Intent intent = new Intent(this,DetailActity.class);
intent.putExtra("index",index);
startActivity(intent);
}
}
}
I tried reading documentation but could not get how to solve this. Any help will be appreciated. Thank You.
If you have only added android support jar, you must add the whole Appcompat project to your workspace:
right click -> IMport -> Android -> Project from Existing Source
Then go to SDK Folder where you installed and find extras/android/support/v7/appcompat
and add this whole Library.
It seems like you are missing resources needed for ActionBarActivity

Runtime error while switching between activities in Android

I got a runtime error when I press the button that should change the activity:
package com.example.LocationTracker;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
public class LocationTracker extends Activity{ /** Called when the activity is first created. */
Button btn_Tracker;
Button btn_Display_Map;
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
btn_Tracker = (Button)findViewById(R.id.btn_Tracker);
btn_Tracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setContentView(R.layout.trackeractivity);
Intent myIntent1 = new Intent(view.getContext(), TrackerActivity.class);
context.startActivity(myIntent1);
}});
}
class TrackerActivity extends Activity {
//Your member variable declaration here
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackeractivity);
}
}
I added everything right in the maniefest file
<activity android:name=".TrackerActivity" android:label="#string/app_name"/>
<activity android:name=".DisplayMapActivity" android:label="#string/app_name"/>
</application>
Any idea?
I think TrackerActivity needs to be public, which means it will need to be in its own file as well.
You shouldn't be using getApplicationContext() to start activities. Every activity is a context, so having a member instance of Context should not be necessary. Try re-writing the onClick method of your OnClickListener like this
public void onClick(View view) {
Intent myIntent1 = new Intent(LocationTracker.this, TrackerActivity.class);
LocationTracker.this.startActivity(myIntent1);
}});
Also, refer to this documentation for when to use the application context.

Categories