I am trying to put 2 different layouts in my android application
and that on value in register using shared preferences
this is my code .. it stopped once i click logo
Part of
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs;
//String register;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.main_activity);
}
else
{
setContentView(R.layout.about);
}
}
the LogCat error
FATAL EXCEPTION: main
Process:com,cpcs,irissystem, PID:31749
java.lang.RuntimeExeption : unable to start activity componentInfo
{......MainActivity }:java.lang.NullPointerException
at android.app.ActivityThread.preformLaunchActivity(ActivityThread.java2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java2386)
At android.app.ActivityThread.access$900(ActivityThread.java:169)
At android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
At android.os.Handler.dispatchMeesage(Handler.java:102)
At android.os.looper.loop(LooperLjava:136)
At android.app.ActivityThread.Main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$methodAndArgsCaller.run....:1268)
Activity Class
public class MainActivity extends Activity {
public static String register;
private static final int CAMERA_REQUEST = 1888;
public static String IrisCode;
String IrisCode2;
ImageView imageView;
private static Bitmap eyeImage;
EditText passwordText;
public static float perc ;
SharedPreferences prefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.main_activity);
}
else
{
setContentView(R.layout.about);
}
Toast.makeText(MainActivity.this,"befroe: "+ register, Toast.LENGTH_LONG).show();
Button InstructionButton = (Button) findViewById(R.id.inst);
Button enrollButton = (Button) findViewById(R.id.enroll);
Button verifyButton = (Button) findViewById(R.id.ver);
passwordText = (EditText) findViewById(R.id.pass);
if(register.equalsIgnoreCase("no")){
verifyButton.setEnabled(false);
}
enrollButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Go to the next page
//if Not registered before go to start to enter data
//else go to setting
Intent i = new Intent();
if(register.equalsIgnoreCase("no")) {
i = new Intent (MainActivity.this,start.class);
startActivity(i);
}
else
{
i = new Intent (MainActivity.this,setting.class);
startActivity(i);
}
}
});
InstructionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Go to the instruction page
Intent i = new Intent (MainActivity.this,instruction.class);
startActivity(i);
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if("0000".equals(passwordText.getText().toString())){
String same="aothorizes acess";
Toast.makeText(MainActivity.this, same, Toast.LENGTH_LONG).show();
Intent i = new Intent (MainActivity.this,setting.class);
startActivity(i);
}
}
});
}
Manifest
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cpcs.irissystem.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".start" />
<activity
android:name=".setting" />
<activity
android:name=".aboutus" />
<activity
android:name=".instruction" />
<activity
android:name=".setting2"/>
<activity
android:name=".ver"/>
</application>
// Try this way,hope this will help you...
SharedPreferences prefs;
String register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
register=prefs.getString("register", "no");
if(register.equalsIgnoreCase("no")) {
setContentView(R.layout.activity_main);
}
else
{
setContentView(R.layout.activity_main2);
}
}
Related
What i'm trying to do is this:
Make an alarm app, when the alarmmanager activates, switch to a new activity and open the app (The activity where you can stop the alarm)
What happens now with my code is this:
Alarmmanager goes off, it switches to the new activity but the app does not open. You have to open in manually to see the stop alarm screen. How do i do this?
Mainactivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TimePicker klok = findViewById(R.id.timePicker1);
klok.setIs24HourView(true);
final Button getTimeBtn = findViewById(R.id.getTimeBtn);
final TextView showText = findViewById(R.id.showText);
Intent intent = new Intent(this, AlarmActivation.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
getTimeBtn.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onClick(View view) {
int uur = klok.getHour();
int minuut = klok.getMinute();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, uur);
cal.set(Calendar.MINUTE, minuut);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Toast.makeText(getApplication().getApplicationContext(), "Alarm gaat om " + uur + ":" + minuut,Toast.LENGTH_LONG).show();
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
showText.setText(Integer.toString(uur) + ':' + Integer.toString(minuut));
}
});
}
}
AlarmActivation.java
public class AlarmActivation extends BroadcastReceiver {
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("alarmact", "voor de service");
Intent alarmIntent = new Intent(context, MainActivity2.class);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmIntent);
}
}
MainActivity2.java
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wekkerapp">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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">
<activity
android:name=".MainActivity2"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmActivation"></receiver>
<service
android:name=".WekkerService"
android:enabled="true"/>
</application>
</manifest>
Hope someone can help me a little with this
How about make BroadcastReceiver in your MainActivity2 ?
like this.
public class MainActivity2 extends AppCompatActivity {
public static final String BROAD_TAG = "braodTag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter(BROAD_TAG));
}
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if (isRunningActivity) {
//get Data from intent if you need
}
}
}
};
}
and when you send Data, (AlarmActivation.java)
public class AlarmActivation extends BroadcastReceiver {
MediaPlayer mp;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("alarmact", "voor de service");
Intent braodintent = new Intent(MainActivity2.BROAD_TAG);
LocalBroadcastManager.getInstance(this).sendBroadcast(braodintent);
}
}
I keep getting this build error 'class' or 'interface' expected. Everything I'm finding on SO is saying that my Java class is missing (or has an extra) a "}" I'm getting this error in my AndroidManifest.xml for all my Java activities. Here is the Java code:
public class MainActivity extends AppCompatActivity {
protected static final String TAG = "com.example.grey";
EditText SendValue;
Button SendEditTextValue;
Intent intent;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Log.d(TAG, "onCreate started from Main Activity");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mMenuInflater = getMenuInflater();
mMenuInflater.inflate(R.menu.my_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_setting){
Toast.makeText(MainActivity.this,
"Settings Clicked",
Toast.LENGTH_SHORT).show();
}
if(item.getItemId() == R.id.about_us){
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public void sendButton(View view){
SendEditTextValue = (Button) findViewById(R.id.button1);
SendValue = (EditText) findViewById(R.id.editText1);
SendEditTextValue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), TextBoxMsgSend.class);
intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
startActivity(intent);
}
});
}
public void movieButton(View view){
Intent intent = new Intent(this, ShowMovies.class);
startActivity(intent);
}
public void button7(View view){
CharSequence text = "Two";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
}
public void button8(View view){
CharSequence text = "Three";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
}
public void button9(View view){
CharSequence text = "Four";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart started from Main Activity");
}
#Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause started from Main Activity");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop started from Main Activity");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy started from Main Activity");
}
#Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart started from Main Activity");
}
}
And here is the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.grey">
<uses-permission android:name="android.permission.INTERNET" />
<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.Light.NoActionBar">
<!-- The main/home activity (it has no parent activity) -->
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- A child of the main activity -->
<activity
android:name=".TextBoxMsgSend"
android:parentActivityName=".MainActivity" />
<activity
android:name=".ShowMovies"
android:parentActivityName=".MainActivity" />
<activity
android:name=".RecyclerAdapter"
android:parentActivityName=".MainActivity" />
<activity
android:name=".MovieDetails"
android:parentActivityName=".ShowMovies" />
<activity
android:name=".AboutActivity"
android:parentActivityName=".MainActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.grey.MainActivity" />
</activity>
</application>
</manifest>
I keep getting build errors and am not able to do any commits. It was working fine the other day so I'm not really sure what changed. I can post my other activities if needed.
public class MovieDetails extends AppCompatActivity{
private static final String TAG = "MovieDetailsActivity";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_details);
Log.d(TAG, "Started program.");
getGalleryIntent();
}
private void getGalleryIntent(){
if(getIntent().hasExtra("image_url") && getIntent().hasExtra("image_name")){
Log.d(TAG,"Found intents.");
String imageURL = getIntent().getStringExtra("image_url");
String imageName = getIntent().getStringExtra("image_name");
String imageYear = getIntent().getStringExtra("image_year");
String imageDirector = getIntent().getStringExtra("image_director");
String imageDescription = getIntent().getStringExtra("image_description");
setImage(imageURL, imageName, imageYear, imageDirector, imageDescription);
}
}
private void setImage(String imageURL, String imageName, String imageYear, String imageDirector, String imageDescription){
TextView name = findViewById(R.id.title);
name.setText(imageName);
TextView year = findViewById(R.id.year);
year.setText(imageYear);
TextView director = findViewById(R.id.director);
director.setText(imageDirector);
TextView description = findViewById(R.id.description);
description.setText(imageDescription);
ImageView image = findViewById(R.id.image);
Glide.with(this).asBitmap().load(imageURL).into(image);
}
}
Try a period before MainActivity where it's declared in the manifest.
I'm beginner in Android Studio. My app allows me to add users(fn,ln,adrs,Uri::ImageUri) and list them and view their info.
I want to be able to display one user's info when clicking on my listview.
The problem is that i only get a blank space instead of my desired imageview.
My main activity :
public class MainActivity extends AppCompatActivity {
public static ArrayList <User> users =new ArrayList <User>();
public static User userSelected;
public static ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
mListView = (ListView) findViewById(R.id.List);
}
public void InputInfo(View view) {
Intent AddEmployee = new Intent(this, NewEmployeeInfo.class);
startActivityForResult(AddEmployee,0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(0, resultCode, data);
finaltest =NewEmployeeInfo .targetUri ;
UserAdapter useradapter;
useradapter = new UserAdapter (MainActivity .this, android.R.layout.simple_list_item_1, users ) ;
mListView.setAdapter(useradapter );
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
Intent appInfo = new Intent(MainActivity.this, DisplayInfo.class);
User temp= users.get(position );
userSelected =new User(temp.first_name ,temp.last_name,temp.adr,temp.url );
startActivity(appInfo);
}
});
}
}
My second one (where the user inputs the info :
public class NewEmployeeInfo extends AppCompatActivity {
public ImageView imageview;
public ImageView targetImage;
public User u;
public static Bitmap photo;
public static Uri targetUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_employee_info);
}
public void takePhoto(View view) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
}
public void pickphoto(View view) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
imageview = (ImageView) this.findViewById(imageView);
resultphoto(requestCode, resultCode, imageReturnedIntent);
}
public void Save(View view) {
Intent SaveDataIntent = new Intent(this, MainActivity.class);
EditText FN = (EditText) findViewById(R.id.editText);
EditText LN = (EditText) findViewById(R.id.editText5);
EditText ADDRESS = (EditText) findViewById(R.id.editText6);
String firstname = FN.getText().toString();
String lastname = LN.getText().toString();
String address = ADDRESS.getText().toString();
if (firstname.length() == 0 || lastname.length() == 0 || address.length() == 0) {
Toast.makeText(this, "No Emty Fields!!",
Toast.LENGTH_LONG).show();
return;
}
u = new User(firstname, lastname, address, targetUri);
users.add(u);
setResult(RESULT_OK, SaveDataIntent);
finish();
}
private void resultphoto(int requestCode, int resultCode, Intent imageReturnedIntent) {
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
targetUri = imageReturnedIntent.getData();
imageview.setImageURI(targetUri);
imageReturnedIntent.getExtras().get("data");
}
break;
case 1:
if (resultCode == RESULT_OK) {
targetUri = imageReturnedIntent.getData();
targetImage = (ImageView) findViewById(imageView);
try {
photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(photo);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}
}
the third and final one(Where the info should be displayed)
public class DisplayInfo extends AppCompatActivity {
public ImageView imageView;
public Bitmap image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_info);
TextView fn = (TextView) findViewById(R.id.firstname);
TextView ln = (TextView) findViewById(R.id.lastname);
TextView adr = (TextView) findViewById(R.id.address);
imageView = (ImageView) findViewById(R.id.imageView2);
fn.setText(MainActivity.userSelected.first_name);
ln.setText(MainActivity.userSelected.last_name);
adr.setText(MainActivity.userSelected.adr);
imageView.setImageURI(MainActivity.userSelected.url ) ;
This error keep showing up
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/810 from pid=12644, uid=10280 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
Though I included these permissions in my manifest file:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Ask runTime permissions
tutorial
My problem was in the Manifest file;
It was
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewEmployeeInfo">
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".DisplayInfo" />
<activity android:name=".UserAdapterActivity"></activity>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</application>
I misplaced my permissions. It should've been like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobilonia.employeesapp">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewEmployeeInfo">
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".DisplayInfo" />
<activity android:name=".UserAdapterActivity"></activity>
</application>
</manifest>
I tried to change my Launcher Activity in my Manifest file. After changing it, I went reseted my code of again (re-edited so that it was the same as before). So I just copy pasted this code between my activities
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
After reseting my code the app won't open.
I got an error
java.lang.IllegalArgumentException: AppCompat does not support the current theme features, poiting out on my setContentView(R.layout.activity_main);
I got really frustrated by this problem cause I didn't change anything in my code (it is excaclty as it used to be when it was working)
Here is my manifest file code
<manifest package="com.inthessaloniki.cityguide"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto">
<!-- versionCode, versionName, minSdkVersion, targetSdkVersion properties are set via Gradle script -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- allows the API to access Google web-based services -->
<!-- maps API needs OpenGL ES 2.0 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="false"/>
<!--
You can easily change the main theme. Just modify application.android:theme attribute.
There are 8 main themes you can use:
Theme.CityGuide.Blue
Theme.CityGuide.Brown
Theme.CityGuide.Carrot
Theme.CityGuide.Gray
Theme.CityGuide.Green
Theme.CityGuide.Indigo
Theme.CityGuide.Red
Theme.CityGuide.Yellow
Don't forget to modify also MainActivity's theme.
-->
<application
android:name=".CityGuideApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.CityGuide.Indigo"
tools:replace="android:icon">
<!--
Themes for MainActivity:
Theme.CityGuide.TransparentStatusBar.Blue
Theme.CityGuide.TransparentStatusBar.Brown
Theme.CityGuide.TransparentStatusBar.Carrot
Theme.CityGuide.TransparentStatusBar.Gray
Theme.CityGuide.TransparentStatusBar.Green
Theme.CityGuide.TransparentStatusBar.Indigo
Theme.CityGuide.TransparentStatusBar.Red
Theme.CityGuide.TransparentStatusBar.Yellow
-->
<activity
android:name=".activity.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.CityGuide.TransparentStatusBar.Indigo"
android:launchMode="standard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.PoiDetailActivity"
android:label="#string/title_poi_detail"
android:launchMode="standard"/>
<activity
android:name=".activity.TourDetailActivity"
android:label="#string/title_tour_detail"
android:launchMode="standard"/>
<activity
android:name=".activity.MapActivity"
android:label="#string/title_map"
android:launchMode="standard"/>
<activity
tools:replace="android:configChanges"
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode|smallestScreenSize"/>
<activity
android:name=".activity.DescriptionDetailsActivity"
android:label="#string/title_activity_description_details"
android:theme="#style/Theme.CityGuide.TransparentStatusBar.Indigo">
</activity>
<activity
android:name=".activity.MailFormActivity"
android:label="#string/title_tour_detail"
android:theme="#style/Theme.CityGuide.TransparentStatusBar.Indigo">
</activity>
<provider
android:name=".content.PoiSearchRecentSuggestionsProvider"
android:authorities="com.inthessaloniki.cityguide.content.PoiSearchRecentSuggestionsProvider"
android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="#xml/analytics_global_tracker"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/maps_api_key"/>
</application>
Main Activity Code
public class MainActivity extends AppCompatActivity implements DrawerAdapter.CategoryViewHolder.OnItemClickListener, OnSearchListener
{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ScrimInsetsFrameLayout mDrawerScrimInsetsFrameLayout;
private DrawerAdapter mDrawerAdapter;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private List<CategoryModel> mCategoryList;
public static Intent newIntent(Context context)
{
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupActionBar();
setupRecyclerView();
setupDrawer(savedInstanceState);
// init analytics tracker
((CityGuideApplication) getApplication()).getTracker();
}
#Override
public void onStart()
{
super.onStart();
// analytics
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
#Override
public void onResume()
{
super.onResume();
}
#Override
public void onPause()
{
super.onPause();
}
#Override
public void onStop()
{
super.onStop();
// analytics
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
#Override
public void onDestroy()
{
super.onDestroy();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// open or close the drawer if home button is pressed
if(mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
// action bar menu behaviour
switch(item.getItemId())
{
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfiguration)
{
super.onConfigurationChanged(newConfiguration);
mDrawerToggle.onConfigurationChanged(newConfiguration);
}
#Override
public void setTitle(CharSequence title)
{
mTitle = title;
getSupportActionBar().setTitle("");
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/nexa-bold.otf");
final TextView toolbarName = (TextView) findViewById(R.id.toolbar_title);
toolbarName.setText(mTitle);
}
#Override
public void onItemClick(View view, int position, long id, int viewType)
{
// position
int categoryPosition = mDrawerAdapter.getCategoryPosition(position);
selectDrawerItem(categoryPosition);
}
#Override
public void onSearch(String query)
{
Fragment fragment = PoiListFragment.newInstance(query);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment).commitAllowingStateLoss();
mDrawerAdapter.setSelected(mDrawerAdapter.getRecyclerPositionByCategory(0));
setTitle(getString(R.string.title_search) + ": " + query);
}
private void setupActionBar()
{
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar();
bar.setDisplayUseLogoEnabled(false);
bar.setDisplayShowTitleEnabled(true);
bar.setDisplayShowHomeEnabled(true);
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
}
private void setupRecyclerView()
{
// reference
RecyclerView recyclerView = getRecyclerView();
// set layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
// load categories from database
loadCategoryList();
// set adapter
if(recyclerView.getAdapter()==null)
{
// create adapter
mDrawerAdapter = new DrawerAdapter(mCategoryList, this);
}
else
{
// refill adapter
mDrawerAdapter.refill(mCategoryList, this);
}
recyclerView.setAdapter(mDrawerAdapter);
// add decoration
List<Integer> dividerPositions = new ArrayList<>();
dividerPositions.add(3);
RecyclerView.ItemDecoration itemDecoration = new DrawerDividerItemDecoration(
this,
null,
dividerPositions,
getResources().getDimensionPixelSize(R.dimen.global_spacing_xxs));
recyclerView.addItemDecoration(itemDecoration);
}
private void setupDrawer(Bundle savedInstanceState)
{
mTitle = getTitle();
mDrawerTitle = getTitle();
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/nexa-bold.otf");
final TextView toolbarName = (TextView) findViewById(R.id.toolbar_title);
toolbarName.setText(mTitle);
toolbarName.setTypeface(typeface);
// reference
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_layout);
mDrawerScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.activity_main_drawer);
// set drawer
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayout.setStatusBarBackgroundColor(ResourcesHelper.getValueOfAttribute(this, R.attr.colorPrimaryDark));
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close)
{
#Override
public void onDrawerClosed(View view)
{
toolbarName.setText(mTitle);
getSupportActionBar().setTitle("");
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView)
{
toolbarName.setText(mTitle);
getSupportActionBar().setTitle("");
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
// show initial fragment
if(savedInstanceState == null)
{
selectDrawerItem(0);
}
}
private void selectDrawerItem(int position)
{
long mCategoryId = mCategoryList.get(position).getId();
Fragment fragment = null;
if(mCategoryId==PoiListFragment.CATEGORY_ID_ALL || mCategoryId==PoiListFragment.CATEGORY_ID_FAVORITES ){
fragment = PoiListFragment.newInstance(mCategoryId);
}else{
fragment = SubCategoryListFragment.newInstance(mCategoryId);
}
FragmentManager fragmentManager = getSupportFragmentManager();
String categoryName = mCategoryList.get(position).getName();
fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment, categoryName).addToBackStack(categoryName).commitAllowingStateLoss();
mDrawerAdapter.setSelected(mDrawerAdapter.getRecyclerPositionByCategory(position));
setTitle(categoryName);
mDrawerLayout.closeDrawer(mDrawerScrimInsetsFrameLayout);
}
public void showPOIFragment(long categoryId, String categoryName){
PoiListFragment fragment = PoiListFragment.newInstance(categoryId);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment, categoryName).addToBackStack(categoryName).commitAllowingStateLoss();
if (!TextUtils.isEmpty(categoryName)){
setTitle(categoryName);
}
}
public void showSubCategoryFragment(long categoryId, String categoryName){
SubCategoryListFragment fragment = SubCategoryListFragment.newInstance(categoryId);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment, categoryName).addToBackStack(categoryName).commitAllowingStateLoss();
if (!TextUtils.isEmpty(categoryName)){
setTitle(categoryName);
}
}
private void loadCategoryList()
{
try
{
mCategoryList = CategoryDAO.readAll(-1l, -1l);
}
catch(SQLException e)
{
e.printStackTrace();
}
CategoryModel Thessaloniki = new CategoryModel();
Thessaloniki.setId(PoiListFragment.CATEGORY_ID_THESSALONIKI);
Thessaloniki.setName("Thessaloniki");
Thessaloniki.setImage("drawable://" + R.drawable.ic_category_all);
CategoryModel all = new CategoryModel();
all.setId(PoiListFragment.CATEGORY_ID_ALL);
all.setName(getResources().getString(R.string.drawer_category_all));
all.setImage("drawable://" + R.drawable.ic_category_all);
CategoryModel favorites = new CategoryModel();
favorites.setId(PoiListFragment.CATEGORY_ID_FAVORITES);
favorites.setName(getResources().getString(R.string.drawer_category_favorites));
favorites.setImage("drawable://" + R.drawable.ic_category_favorites);
mCategoryList.add(0,Thessaloniki);
mCategoryList.add(1, all);
mCategoryList.add(2, favorites);
}
private RecyclerView getRecyclerView()
{
return (RecyclerView) findViewById(R.id.activity_main_drawer_recycler);
}
#Override
public void onBackPressed() {
int count = getSupportFragmentManager().getBackStackEntryCount();
if (count == 1) {
finish();
}else{
getSupportFragmentManager().popBackStack();
String name = getSupportFragmentManager().getBackStackEntryAt(count-2).getName();
setTitle(name);
}
// super.onBackPressed();
}
}
I had face similar issue when i had firstly created project in android stdio with API 23 so for fixing these issue try to do some changes.
Please change your activity code first line :-
public class MainActivity extends AppCompatActivity
to these :-
public class MainActivity extends Activity
It had worked for me when i was in same problem.
I'm doing a basic activity of moving from one page to another. Everything was going perfect until I get two pages deep into the app. I used the same exact code for each page class, put the activities in the manifest and made sure all words were spelled correctly, etc., but the intent doesn't do anything when I try to go three pages deep. There are no error messages in the log at all. When I click on a button on the third page, it just turns blue but doesn't move to the next page like the previous pages. Here's my code:
from page 1 to 3:
Page1:
public class MainActivity extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton(); }
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MainMenu.class);
startActivity(intent);
}
});
}
}
Page 2: has more buttons...
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
Button button3;
Button button4;
Button button5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, OneMain.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, TwoMain.class);
startActivity(intent);
}
});
button3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ThreeMain.class);
startActivity(intent);
}
});
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, FourMain.class);
startActivity(intent);
}
});
button5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, FiveMain.class);
startActivity(intent);
}
});
}
}
Page 3:
public class OneMain extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_main);
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Alphabet.class);
startActivity(intent);
}
});
}
}
I also made sure I put in all the imports. Someone help. I'm stuck :(.
Here is the manifest xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.juwar74.alarabic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".MainMenu" >
</activity>
<activity
android:label="#string/app_name"
android:name=".OneMain" >
</activity>
<activity
android:label="#string/app_name"
android:name=".TwoMain" >
</activity>
<activity
android:label="#string/app_name"
android:name=".ThreeMain" >
</activity>
<activity
android:label="#string/app_name"
android:name=".FourMain" >
</activity>
<activity
android:label="#string/app_name"
android:name=".FiveMain" >
</activity>
<activity
android:label="#string/app_name"
android:name=".OneVoc" >
</activity>
</application>
</manifest>
And here is the code for the Alphabet.class
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class Alphabet extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabet);
}
}
in page3 you never called addListenerOnButton() .
public class OneMain extends ActionBarActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_main);
// HERE you missed addListenerOnButton();
}
<activity
android:name=".Alphabet " >
</activity>
You missed this in the manifest.xml
First call addListenerOnButton() in OneMain.java after setContentView.
Then check manifest.
Alphabet.class is not registered in manifest.
<activity
android:label="#string/app_name"
android:name=".Alphabet" >
</activity>
After adding it, make sure you are rendering correct xml in Alphabet.java