I create a personnal component view and when we click on this, an other activity starts. There is my manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.freshkamekentrainement.skrt">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:theme="#style/Splash"
android:screenOrientation="portrait"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation"/>
<activity android:name=".niveaux.LilUziVert_GrowUp"
android:screenOrientation="portrait"
android:configChanges="orientation"/>
</application></manifest>
there is my intent
public class Niveauview extends RelativeLayout {
Intent intentNiveau;
//Code
#Override
public void onFinishInflate() {
super.onFinishInflate();
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
intentNiveau = new Intent(MainActivity.this,LilUziVert_GrowUp.class);
startActivity(intentNiveau);
}
});
}}
I get error:
MainActity is not an enclosing class
Notice that NiveauView and MainActivity isn't in the same package (but they are public). Where does the problem come from? When i try new Intent(this,LilUziVert_GrowUp.class); i have an error too.
You can only access Foo.this if you're in a inner class of Foo. Otherwise you need to be passed in an instance of Foo instead. In the case of a View, every view has an instance of a Context you can get by calling getContext(). So you need to call intentNiveau = new Intent(getContext(),LilUziVert_GrowUp.class);
Related
I want to send and receive remote notification through Firebase but my custom class could not find getApplicationContext() method.
From my understanding, FirebaseMessagingService extends Service, which extendsContextWrapper` which extends Context.
So I don't know getApplicationContext() is not working. Any help will be appreciated!
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle();
String text = remoteMessage.getNotification().getBody();
NotificationHelper.displayNotification(getApplicationContext(), title, text);
}
super.onMessageReceived(remoteMessage);
}
}
Here's my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mealz">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".Activities.UserActivity"></activity>
<activity android:name=".Activities.LoginActivity" />
<activity android:name=".Activities.SignupActivity" />
<activity android:name=".Activities.SearchRecipeActivity" />
<activity android:name=".Activities.RecipeDetailActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Notifications.FirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
You can achieve this in many way one way is to keep a weak refrence of context make a app class extending android.app.application and on it's create method and make a variable of weak refrence of context and assign it to getApplicationContext() and you may use this context anywhere you want.
public class app extends android.app.Application{
private static WeakReference<Context> referenceCntx;
#Override
public void onCreate() {
super.onCreate();
referenceCntx = new WeakReference<>(getApplicationContext());
}
public static Context getApplicationCntx(){
return referenceCntx.get();
}
}
And don't forget to add this line in manifest
<application
android:name=".app"
I have starting building an app in Android studio. I have established the MainPage as the launcher activity in the manifest.xml.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainPage">
android:screenOrientation="portrait"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".courseSelect" />
<activity android:name=".profile1" />
<activity android:name=".stats1" />
<activity android:name=".ReviewRounds" />
<activity android:name=".ReferFriends" />
<activity android:name=".RangeMode" />
</application>
I have double checked that the run configuration is set to 'Default' and yet the app is running a different activity, entitled courseSelect. It is also not running some code correctly on the NumberPicker. Even though I've set the picker to have a min, max, and default, the picker only shows 0 and will not scroll. The two issues seem to be related somehow, in terms of what activity is being run.
this is the courseSelectCode:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
import co.ceryle.segmentedbutton.SegmentedButtonGroup;
public class courseSelect extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select);
//Hole Picker
NumberPicker holePicker = (NumberPicker)findViewById(R.id.holePicker);
holePicker.setMaxValue(18);
holePicker.setMinValue(1);
holePicker.setWrapSelectorWheel(false);
holePicker.setValue(1);
SegmentedButtonGroup sbg = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
sbg.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() {
#Override
public void onClickedButtonPosition(int position) {
// if(position == 0)
}
});
}
}
I tried to set the run configuration specifically to the MainPage activity and it still opens in the courseSelect Page.
EDIT: on request, here is my MainPage.java code:
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select);
Window g = getWindow();
g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
...
}
edit the Mainfest.xml, in order to enforce portrait layout there already:
<activity android:name=".MainPage"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...which makes this code merely useless (styles.xml can also be used for window styles):
Window g = getWindow();
g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
also update setContentView( R.layout.activity_course_select ); to the proper resource.
because it starts the MainPage Activity, but then inflates the wrong XML file.
one "suggested edit" before was to swap the order of setContentView() and the paragraph below ...which I've rejected, because setting it in Manifest.xml appeared more organized (less code).
First of all There is a mistake in your manifest file
you wrote screenOrientation attribute outside the opening tag
<activity android:name=".MainPage">
android:screenOrientation="portrait"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
it should be
<activity
android:name=".MainPage"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and you used wrong xml to setContenView
Try this and error was in 9th line because your code line is outside of the tag:
<activity android:name=".MainPage">
android:screenOrientation="portrait" // error
Do this :
<activity android:name=".MainPage"
android:screenOrientation="portrait"> // After doing this no error
and do also this thing :
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_select); // error
do this :
public class MainPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MAIN_PAGE_ACTIVITY_NAME); // no error
Your launcher activity is MainPage but you are calling the layout of courseselect activity inside MainPage activity's onCreate method on this line
setContentView(R.layout.activity_course_select);
Change it to your MainPage layout
setContentView(R.layout.yourMainPageLayout);
public class ListViewActivity extends AppCompatActivity
{
ListView lv;
String[] ListItems={"AutoCompleteTextViewActivity","CheckboxActivity","EditTextActivity","ListViewActivity","ProgressBarActivity","RadioButtonActivity","RatingBar2Activity","RatngBarActivity","SeekBarActivity","SpinnerActivity","SwitchActivity","TextViewActivity"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter ad=new ArrayAdapter(ListViewActivity.this,android.R.layout.simple_list_item_1,ListItems);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent i = new Intent(ListViewActivity.this, ListItems[position].getClass());
startActivity(i);
}
});
}
}
i am current learning android studio.i made an app through which i can open my previously created apps by choosing them from a listview widget.it shows an error in the intent line.please tell me where i've have gone wrong.
MY MANIFEST FILE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.re1">
<application
android:allowBackup="true"
android:banner="#mipmap/ic_launcher_round"
android:icon="#mipmap/ic_launcher_round"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".TextViewActivity" />
<activity android:name=".EditTextActivity" />
<activity android:name=".RadioButtonActivity" />
<activity android:name=".CheckboxActivity" />
<activity android:name=".RatngBarActivity" />
<activity android:name=".RatingBar2Activity" />
<activity android:name=".ProgressBarActivity" />
<activity android:name=".SeekBarActivity" />
<activity android:name=".SwitchActivity" />
<activity android:name=".SpinnerActivity"/>
<activity android:name=".AutoCompleteTextViewActivity">
</activity>
<activity android:name=".ListViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Add your activity name to your manifest file
<activity android:name=".ListViewActivity"></activity>
Explanation:
I have mainActivity in which i implemented a navigationDrawer. There are multiple fragment.In first,fragment namely HomeFragment. In homeFragment i am using viewpager and listview. in my viewpager there is two things first, if match is started then it's goes to summaryCard activity. second if match no started it's goes to notstartedmatchdetail activiy.
when match is starting first i goes to summarycard activity.in summarycard activity i put the button. when i press the button in summarycard it's directly goes to fullscorecard.e.g. homeFragment->summarycard->fullscorecard.
In listview, it seen the detail of the match which was completed.Here i am directly goes to fullscorecard.e.g. homefragment->fullscorecard.
Situation:
While i goes to homefragment->fullscorecard
After pressing back button of fullscorecard it goes to summarycard instead of homefragment.
Here is my homeFragment where i called an adapter
LiveAdapter adapterTabRecent = new LiveAdapter(getContext(), lst_obj, PagerLength, list_status, lst_key);
adapterTabRecent.notifyDataSetChanged();
pagerRecentMatches.setAdapter(adapterTabRecent);
pagerRecentMatches.setCurrentItem(current_pos);
ScheduleAdapter CmAdapter = new ScheduleAdapter(getContext(), arr_completed, lst_key2);
CmAdapter.notifyDataSetChanged();
completed_listview.setAdapter(CmAdapter);
Above both the adapter called in homefragment for started match it called liveAdapter and for completed match is called ScheduleAdapter.
Here, is my live adapter
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key=lst_key.get(position);
String status_value=list_status.get(position);
if(status_value.equals("notstarted")){
Log.e("MATCH_KEY",""+key);
Intent intent=new Intent(context,NotStartedMatchDetails.class);
intent.putExtra("mainobj", lst.get(position).toString());
context.startActivity(intent);
}
if(status_value.equals("started")){
Log.e("MATCH_KEY",""+key);
MainActivity mainActivity=(MainActivity)context;
Intent intent=new Intent(context,SummaryCard.class);
intent.putExtra("Key", key);
intent.putExtra("access_token",mainActivity.getMyData());
context.startActivity(intent);
}
}
});
Here is a code for listview where match is completed.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
match_key = arr_matchKey.get(position);
Log.e("Schedule complted key", match_key);
MainActivity activity = (MainActivity) context;
GetValue gv = new GetValue(context);
gv.setFullUrl(match_key);
gv.setAccessToken(activity.getMyData());
gv.execute();
}
});
Here is my manifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.angelnx.cricvilla.cricvilla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMaterialTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".NotStartedMatchDetails"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity">
</meta-data>
</activity>
<activity
android:name=".SummaryCard"
android:screenOrientation="portrait"
android:theme="#style/MyMaterialTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".ScoreCard"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SummaryCard" />
</activity>
<activity
android:name=".PlayerInfoDetails"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".FullScore"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".PlayerIccStats"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
Consider fullscorecard as Scorecard in manifiest.
This problem is cause only in ice-cream sandwich lg model.
Otherwise it' working correctly in another devices.
Please help me to solve this problem.
Im very new to android development so please bear my ignorance.
I created a Splash screen before loading my main activity. Splash is working fine but what causes the problem is the main activity, it keeps on instantiating.
Splash.java
public class Splash extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
final AdController optinController = new AdController(
getApplicationContext(), "SECTION_ID");
final Splash splash = this;
optinController.loadOptin((Activity) splash, "SECTION_ID",
new AdOptinListener() {
public void onAdOptin() {
// once optin process is complete, continue to main app activity
launchMain();
}
}
);
}
public void launchMain() {
finish();
Intent myIntent = new Intent(Splash.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
}
}
MainActivity.java
public class MainActivity extends Activity {
private AdController myController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myController = new AdController(getApplicationContext(), "SECTION_ID");
myController.loadNotification();
}
}
In the manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I used android:launchMode="singleInstance" but still it keeps on reinstantiating.
Please help. Thanks in advance.
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//|Window.FEATURE_INDETERMINATE_PROGRESS
setContentView(R.layout.splash);
final Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},1000);
}}
It will work , i din get you exactly by instatiating.Also do no use single instance,you are not supposed to require it here.
Yes, following is my snippet of manifest file , just use a timer for your splash screen and move to your main class after specific time.
activity android:name=".Splash"
android:screenOrientation="portrait" android:label="#string/app_name">
i think you have to remove the intent flag Intent.FLAG_ACTIVITY_SINGLE_TOP
In manifest.xml, set category as LAUNCHER which activity you want to launch first and to Other set as DEFAULT as given in below example.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>