I have this mini app insided MainActivity.java: class Drawer extends BaseDrawerActivity…
- User
I have this mini app insided MainActivity.java: class Drawer extends BaseDrawerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
}}
public class MainActivity extends BaseDrawerActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }}
I also have a menu for it. If I put "setContentView(R.layout.activity_main); initializeDrawer();" in MainActivity, it shows the menu, if I put it into Drawer class, it doesn't. Why? BTW here's my BaseDrawerActivity: package com.HidiStudios.BeST;
import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; import android.view.MenuItem; import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import java.util.Objects;
public abstract class BaseDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected DrawerLayout drawerLayout; protected NavigationView navigationView; protected ActionBarDrawerToggle drawerToggle; // Initialize the drawer in the derived activity’s ‘onCreate’ method. protected void initializeDrawer() { drawerLayout = findViewById(R.id.drawer_layout); navigationView = findViewById(R.id.nav_view); drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close); drawerLayout.addDrawerListener(drawerToggle); drawerToggle.syncState(); Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); navigationView.setNavigationItemSelectedListener(this); } // Handle drawer item selected in one place. @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // Your navigation item handling code… int id = item.getItemId(); navigationView.bringToFront(); if (!(this instanceof BaseDrawerActivity) && id == R.id.home) { Toast.makeText(BaseDrawerActivity.this, "Home pressed", Toast.LENGTH_SHORT).show(); finish(); } else if (!(this instanceof LeaderboardActivity) && id == R.id.leaderboard) { Toast.makeText(BaseDrawerActivity.this, "Leaderboard pressed", Toast.LENGTH_SHORT).show(); if (1==2//MainActivity.changedSinceSynced ) {/* MainActivity.changedSinceSynced = false; //sync is done here, not using onStop MainActivity.saveToDB(false, new MainActivity.DbOperationCompleteListener() { @Override public void onDbOperationComplete() { openLeaderboardActivity(); } });*/ } else { openLeaderboardActivity(); } } else if (id == R.id.chatroom) { Toast.makeText(BaseDrawerActivity.this, "chatroom pressed", Toast.LENGTH_SHORT).show(); }/*else if (id == R.id.login) { Toast.makeText(BaseDrawerActivity.this, "Login pressed", Toast.LENGTH_SHORT).show(); }*/else if (id == R.id.share) { share(); } else if (id == R.id.chat_with_me) { Toast.makeText(BaseDrawerActivity.this, "chat_with_me pressed", Toast.LENGTH_SHORT).show(); } // Close drawer after item is selected. drawerLayout.closeDrawer(GravityCompat.START); return false; // return true if you want to indicate the item was selected. } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } private void openLeaderboardActivity() { Intent intent = new Intent(this, LeaderboardActivity.class); startActivity(intent); } - Assistant
- User
Ok, now I have one class. public class MainActivity extends BaseDrawerActivity
The menu works, but i need to use functions from StatsUtil.java: package com.HidiStudios.BeST.Utilities;
import android.content.SharedPreferences; import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.HidiStudios.BeST.R;
public class StatsUtil extends AppCompatActivity {
- Assistant
- User
For example, I have a function like this in MainActivity: private void showRewardedAds() {
if (mRewardedAd!=null) { mRewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { Log.d("TagDB", "Reward earned!"); amount += getResources().getInteger(R.integer.onRewardedToggle); updateCoins(); changedSinceSynced = true; StatsUtil.rewardedWatchedCount++; StatsUtil.updateStats(this, statsTextView); } }); } else { Toast.makeText(MainActivity.this,"Rewarded ad is not ready yet. Try again!", Toast.LENGTH_SHORT).show(); } }But for "StatsUtil.updateStats(this, statsTextView);" it says error: incompatible types: cannot be converted to Context StatsUtil.updateStats(this, statsTextView); ^
- Assistant
The error you're encountering is due to the
thiskeyword used inside theOnUserEarnedRewardListeneranonymous class, which refers to the instance of the anonymous class itself rather than theMainActivity. This anonymous class does not represent aContextwhich is why the compiler complains when you try to passthisas aContext.To resolve this issue, you need to reference the
MainActivityexplicitly when you're inside the anonymous class. You can do this by usingMainActivity.thisinstead of justthis:private void showRewardedAds() { if (mRewardedAd != null) { mRewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { Log.d("TagDB", "Reward earned!"); amount += getResources().getInteger(R.integer.onRewardedToggle); updateCoins(); changedSinceSynced = true; StatsUtil.rewardedWatchedCount++; // Use MainActivity.this to refer to the MainActivity context StatsUtil.updateStats(MainActivity.this, statsTextView); } }); } else { Toast.makeText(MainActivity.this, "Rewarded ad is not ready yet. Try again!", Toast.LENGTH_SHORT).show(); } }