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.
Related
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.
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>
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);
}
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..
I use my Main Activity to paint Text. Now Ive a Service in Background that listens to a bluetooth device. If a button on the device is pressed I insert values in the local database and want to repaint the correct value in my Main Activity. Everything works fine - just the repaint does not work, because i cant call the invalidate() method from my Service.
I tried with a BroadCastReceiver but i think i misunderstood something.
This is my Method in my Service "ControllerService"
private void insertToDB(int iWert, char c) {
if(zt.getJoystickMoved().x == iWert) {
sendOrderedBroadcast(i,null);
Log.i("Broadcast","send!");
//in this block i want to notify the Activity
}
}
Here is my Main Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(resetReceiver, new IntentFilter("my.intent.INTENT_NAME"));
}
public BroadcastReceiver resetReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast", "got Broadcast!");
MainActivity.this.cc.invalidate();
//cc.invalidate() repaints my Activity
//cc is a View with the paint Logic inside
this.setResultCode(Activity.RESULT_OK);
}
}
My Broadcast Receiver in my Main Activity does not receive anything ...
I hope someone could help me with my Problem.
In my AndroidManifest is this code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.countV1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission
android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="com.test.data.ListHelper">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="my.intent.INTENT_NAME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="PreferencesActivity" android:name="PreferencesActivity"></activity>
<activity android:name="ZtPreferencesActivity" android:label="ZtPreferencesActivity"></activity>
<activity android:name="customizePreferences"></activity>
<activity android:name="ControllerActivity"></activity>
<service android:name="com.test.services.ControllerService"></service>
<activity android:name="BatteryActivity"></activity>
</application>
</manifest>