Class integration in Manifest: application stopped - java

My manifest.xml raises error for ".RoleActivity". But If I replace my ".roleActivity" with others for checking, they all are okay. Here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zobaed.androidlogin" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".RoleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DoctorLoginActivity">
</activity>
<activity android:name=".PatientLoginActivity">
</activity>
</application>
</manifest>
Here is my RoleActivity. Tried to write switch case here.
public class RoleActivity extends AppCompatActivity {
private Button btnPatient;
private Button btnDoctor;
private Button btnGuest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in_role);
btnPatient = (Button) findViewById(R.id.btpatient);
btnDoctor = (Button) findViewById(R.id.btdoctor);
btnGuest = (Button) findViewById(R.id.btguest);
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btdoctor: {
Intent i = new Intent(getApplicationContext(), DoctorLoginActivity.class);
startActivity(i);
break;
}
case R.id.btpatient: {
Intent i = new Intent(getApplicationContext(), PatientLoginActivity.class);
startActivity(i);
break;
}
}
}
}

implement onClickListner in RoleActivity class and change code to
btnPatient.setOnClickListener( this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);

Your activity is not implementing View.OnClickListener. Unless you implement View.OnClickListener on your activity you cannot cast the activity as an OnClickListener. That is why you are getting error, probably a ClassCastException
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
implement View.OnClickListener on your activity. Change
public class RoleActivity extends AppCompatActivity
to
public class RoleActivity extends AppCompatActivity implements View.OnClickListener
and then you can remove that casting
btnPatient.setOnClickListener(this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
if you are not implemeting View.OnClickListener on your activity you can add the click listener as an anonymous inner class to handle clicks on views

Related

Trying to Make Pop-Up Window/Overlay in Android Studio (Java)

I am trying to create a pop-up window/overlay in Android Studio with 2 activities. When a certain button (the green plus button in the first picture below) is pressed, it will start a second activity at a smaller size with a different layout (.xml) file. When the second activity is declared in the AndroidManifest.xml, it uses a custom theme I have created to make the first activity appear under it, however, it is not working properly.
Here are pictures of both activites:
First Activity -
Second Activity -
Here is the code:
MainActivity -
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Util.initScreenRes(this); // initializes screen resolution for later pop-up window sizing
}
public void ibAddOnClick(View view){
startActivity(new Intent(MainActivity.this, AddPopUpActivity.class));
}
}
AddPopUpActivity -
public class AddPopUpActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_popup_activity);
getWindow().setLayout((int) (Util.screenRes.x * 0.8), (int) (Util.screenRes.y * 0.5));
}
}
Util -
public class Util {
public static Point screenRes;
public static void initScreenRes(Activity a){
final DisplayMetrics dm = new DisplayMetrics();
a.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenRes = new Point(dm.widthPixels, dm.heightPixels);
}
}
Lastly, here is the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="App.ProgressTracker">
<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.ProgressTracker">
<activity
android:name="App.FrontEnd.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.ProgressTracker.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="App.FrontEnd.AddPopUpActivity"
android:label="ProgressTracker"
android:theme="#style/Theme.ProgressTracker.PopUp">
</activity>
</application>
</manifest>
For reference, I have used this tutorial on YouTube for the way I went about this:
https://www.youtube.com/watch?v=fn5OlqQuOCk&ab_channel=FilipVujovic
I'd appreciate any help on this. Thank you in advance!
In the AndroidManifest.xml set the theme of the pop up activity to
android:theme="#android:style/Theme.Dialog"

Getting the same screen when calling startActivity()

I'm programming an android app with Android Studio where Imagebutton opens a new screen so that the user can proceed to the next step. Imagebutton is working and new screen opens, but it isn't opening second activity but the same one.
I'm new to android and java programming, so please tell me if other codes are necessary for solving the problem and sorry if I shared unnecessary code.
I have created startActivity with intent.
Here are my current codes of MainActivity java-file and AndroidManifest xml-file:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
final ImageButton imgButton = findViewById(R.id.simpleImageViewHowdoglearnspackage);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobidogi">
<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=".SecondActivity"
android:label="#string/title_activity_second" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Changing activity crashes

My app crashes when I try to navigate to another activity. Why does that happen?
I'm able to start the other activity when I launch it at first so there's no problem in the CheckUsernameActivity.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
Try This way. I think it will help you.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
I didn't have my new activity defined in my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimsumdev.runk" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".activity.CheckNumberActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.CheckUsernameActivity" >
<!--Default Intent Filter-->
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".activity.HomeActivity" >
</activity>
</application>
</manifest>

Unable to start an activity on button click

I have a button on main activity which onclick is supposed to start a activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v){
if(v.getId()==R.id.lbutton)
{
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
}
lbutton is the id of button
Display.java
public class Display extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondsc);
}
}
secondsc.xml is a layout file which contains content for the new activity
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".Display">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
remove intent-filter to DisplayActivity in your manifest file:
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And register your clickListener button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.lbutton).setOnClickLIstener(new OnClickLIstener (){
void onClick (View v) {
onButtonClick(v);
}
});
}
You need to add an event listener to the button. You can either implement the View.OnClickListener interface and implement the onClick method (good if you have multiple buttons that need listeners), or use an anonymous class:
btn.setOnClickListener(new View.OnClickListener() {
onClick (View v) {
}
}
Are you sure that the OnButtonClick is being executed? I recommend doing it this way:
Button lButton = (Button) findViewById(R.id.lButton);
lButton.setOnClickListener(new OnClickListener() {
onClick(View view) {
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
-Daniel

App randomly crashing, MainActivity session error occurs

sometimes the application does run but its very slow and messes up. I attached the main activity and the manifest, I think it is one of those that is making the application mess up. Sometimes it gives me this error : Session 'MainActivity': error.
Any help would be greatly appreciated.
Thank you.
Main Activity
import Graphics.MyGLSurfaceView;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
//Runs before the application is created
private Button mCampaignButton;
private Context context = this;
private GLSurfaceView mGLView;
//When the application is created
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//New thread to perform database creation / filling
Runnable runnable = new Runnable(){
public void run(){
//Instantiate a GameDatabase object (this activity)
final GameDatabase gDB = new GameDatabase(context);
gDB.fillGameDatabase();
}
};
Thread myThread = new Thread(runnable);
myThread.start();
mGLView = new MyGLSurfaceView(this);
setContentView(mGLView);
//Keeps screen on so it doesn't fall asleep
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Finding button by button id after application is created
mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
//Checks if the campaign button is clicked
mCampaignButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent to go from main activity to campaign Level Select Activity
final Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
startActivity(intent);
}
});
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tekvision.codedecrypter">
<!-- Tell the system this app requires OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".CampaignSelectLevel"
android:label="#string/title_activity_campaign_select_level" >
</activity>
<!-- Anagrams.Modern Era is to get java class from different package-->
<activity android:name="anagrams.Modern_Era"
android:label="Modern_Era">
</activity>
</application>
Make sure that you are declaring all the permissions in manifest file required by your application i.e.
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
for more details check this: uses-permission

Categories