Android unexcpected error - java

I am a beginner of Android and i need a bit hints and help. I am using following code
public class WifiHotSpotActivity extends Activity {
private Button adnew = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
adnew = (Button) findViewById(R.id.addNewBtn);
adnew.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(v.getContext(), addNew.class);
startActivityForResult(myIntent, 0);
}
});
}
The error i am receiving is
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kahaf.wifiHotSpot/com.kahaf.wifiHotSpot.addNew}; have you declared this activity in your AndroidManifest.xml?
if anyone can tell me what is the problem.

You should define your activity in the manifest file. Here's an example how your manifest should look after the addition of that activity:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foo.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:label="#string/app_name"
android:name=".FooActivity"
android:configChanges="keyboardHidden">"
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YourActivityNameHere"
android:label="#string/app_name"
>
</activity>
<activity
android:name=".AnotherActivity"
android:label="#string/app_name"
>
</activity>
</application>
</manifest>

put this to AndroidManifest.xml
<activity android:name=".WifiHotSpotActivity"/>

It looks like you've not defined the Activity Element in the manifest xml for WifiHotSpotActivity. Without this you will be unable to launch this activity.

go to manifest the application tag.. add activity "addNew" which you are calling in button click..

Related

Problem when I try to open another window from a button the app closes completely "Android Studio"

i'm a student and i'm working on a mobile app.
The probleme is that my app was working, but now when i click on the button to open another window it closes.
Here is my code:
`
public class MainActivity extends AppCompatActivity {
Button b_inscrire_etudiant;
Button b_inscrire_enseignant;
Button b_inscrire_admin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_inscrire_etudiant = findViewById(R.id.b_inscrire_etudiant);
b_inscrire_enseignant = findViewById(R.id.b_inscrire_enseignat);
b_inscrire_admin = findViewById(R.id.b_inscrire_admin);
b_inscrire_etudiant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionEtudiant.class);
startActivity(i);
}
});
b_inscrire_enseignant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionEnseignant.class);
startActivity(i);
}
});
b_inscrire_admin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this , InscriptionAdmin.class);
startActivity(i);
}
});
}
}
`
I worked as usual "using Intent" besides it worked well until now.
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myusto">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyUsto"
tools:targetApi="31">
<activity
android:name=".InscriptionEnseignant"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".InscriptionEtudiant"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".InscriptionAdmin"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".Connexion"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
ps: I solved the problem, by the way two were missing
Thank you for the answers.
It's true, it was missing a part of the code which was not written in the manifest file

Android application failed to start activity

I want to know why it cannot open up new java class.
When I try to start new activity, it will be crash.
public class MainActivity extends ActionBarActivity {
EditText emailEt;
TextView registerET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEt = (EditText) findViewById(R.id.email);
registerET = (TextView) findViewById(R.id.register);
registerET.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("clicks","You Clicked register page");
Intent myIntent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(myIntent);
MainActivity.this.finish();
}
});
}
public void OnLogin(View view) {
String username = emailEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username);
}
}
It's possible manifests problem? Am I put it wrong position for the activity?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
android:allowBackup="true"
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>
</application>
<activity android:label="#string/app_name" android:name=".RegisterActivity"/>
</manifest>
It look like can be open up new java class, but it still display MainActivity.
Error Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a20_1discussboard, PID: 2194
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'register'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Put proper Menifest format:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
android:allowBackup="true"
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:label="#string/app_name"
android:name=".RegisterActivity"/> //<---here
</application>
</manifest>
Don't call finish(), this caused your crash:
registerET.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("clicks","You Clicked register page");
Intent myIntent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(myIntent);
//remove finish();
}
});
Try declared activity in application in your AndroidManifest.xml like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a20_1discussboard">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>"
<application
android:allowBackup="true"
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:label="#string/app_name"
android:name=".RegisterActivity"/>
</application>
</manifest>
Be sure that you add RegisterActivity to your AndroidManifest.xml inside <application> tag:
<application ...>
...
<activity
android:label="#string/app_name"
android:name=".RegisterActivity"/>
</application>
Delete this line from onCreate method: MainActivity.this.finish();
If you want to stop or pause MainActivity, use:
#Override
public void onPause() { }
or
#Override
public void onStop() { }
in MainActivity class.
Thanks all.
Add RegisterActivity to AndroidManifest.xml inside <application> tag and remove MainActivity.this.finish()
It is working now.

How to call an Activity class from non-Activity in Android

I am trying to build library for android, in my library i am trying to call a Activity class from non-Activity class.
This is my non-Activity class
public class Library1 {
Activity activity;
public Library1(Activity activity){
this.activity = activity;
}
public void captureImage() {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
Intent i1 = new Intent ( activity, Aa.class);
context.startActivity(i1);
}
}
This my Activity class which i want to call
public class Aa extends Activity {
public void someMethod()
{
}
}
But when i call Activity class from non-Activty it is showing following error and application is crashing
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.example.androidcamera3/com.example.library1.Aa}; have you declared this
activity in your AndroidManifest.xml?
This is my AndroidManifesto.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.library1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.library1.Library1"
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="com.example.library1.Aa" android:label="#string/app_name" />
</application>
There is a spell mistake. [ acivity => activity ]
Change
<acivity android:name="com.example.library1.Aa" android:label="#string/app_name" />
to
<activity android:name="com.example.library1.Aa" android:label="#string/app_name" />
Activity is defined in package com.example.library1, however it is launched as com.example.androidcamera3/com.example.library1.Aa.

Apk android ActivityNotFoundException

I want to run an implicitly tried with the following code:
MenuActivity.java
public class MenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button btn = (Button) findViewById(R.id.btnVideo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri intentUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pp);
intent.setDataAndType(intentUri,"audio/mp3");
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.powerpump.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MenuActivity" />
</application>
You need to specify more parameters in AndroidManifest.xml?
Thanks.
I guess you simply have to register your MenuActivity in you AndroidManifest.xml as demonstrated here.
Looking at your code, it looks like you created a MenuActivity class after creating an android project with default settings and MenuActivity is your main activity(Launcher) class.
Just replace the MainActivity with MenuActivity in your AndroidManifest.xml file and delete
<activity android:name=".MenuActivity" />
OR
Replace the class name to MainActivity from MenuActivity in your java code as follows
public class MainActivity extends Activity {
......
OR
use this as your android manifest.xml file
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/iconopp14"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="com.powerpump.MenuActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Intent To Start Activity Not Working

I am trying to start an android activity name 'TheGame' from the activity 'SelectionFragment' with a button who's ID is 'startGame'
The SelectionFragment file's public class is :
public class SelectionFragment extends Fragment {
This is the code that contains the Intent:
public void startTheGame (View view) {
Intent intent = new Intent(this, TheGame.class);
startActivity(intent);
}
And this is what my Manifest file looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexlamond.higherorlower"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/highlowlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.alexlamond.higherorlower.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>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity android:name="com.facebook.LoginActivity" >
</activity>
<activity
android:name="com.alexlamond.higherorlower.TheGame"
android:label="#string/title_activity_the_game" >
</activity>
</application>
My issue is that the Intent will not work, when I got to Eclipse's suggestion of how to fix it, it says:
The constructor Intent(SelectionFragment, Class) is undefined
Your onClick attribute must match by exact naming to a public method that has the same name. You have attribute android:onClick="TheGame" and the method signature is public void startTheGame (View view). Change the onClick attribute value to "startTheGame".
However, I would rather use View.OnClickListener instead of setting the onClick attribute because:
On the long run you may reuse/move/change this layout and you will end-up with the same error.
You might do some refactoring and you might change this method by accident or not, and the compiler will not complain.
Setting this type of linkage between UI and model seems to me a violation of MVC.
In case you are starting activity from your fragment, try this:
public void startTheGame (View view) {
Intent intent = new Intent(getActivity(), TheGame.class);
startActivity(intent);
}

Categories