Error while using PackageManager in Android - android

I got a problem in one Android Application which I am working in which I am getting error when I use PackageManager.
The Application is as follows:
Home.java
package com.main.home;
public class Home extends Activity
{
//onclicking a Button say "send"
send.onClickListener()
{
public void onClick() {
Intent i =new Intent();
i.setClassName("com.main.home", "com.main.home.home1");
startActivity(i);
}
}
Home1.java
package com.main.home;
import com.andr.resulting.Result ;
public class Home1 extends Activity {
EditText et=(EditText)findViewById(R.id.e1);
//Clicking on forwardButton in that onClick()
public void onClick()
{
String s[] = {e1.getText().toString(),""};
//Calling a method in a class Result of another package which is not activity
Result.finalfunc(Home1.this,s);
}
Result.java
package com.andr.resulting; //different package
public class Result {
public static void finalfunc(Activity act,String[] re) ...
//Here I want to get the details of this particular class's package (com.andr.resulting) using PackageManager
// I tried like this:
Result.this.getPackageManager().getApplicationInfo(Result.getPackageName(),0))
I am getting error getPackageManager() does not exists in this class file.
How do I solve this issue? I will be eagerly waiting for valuable reply.
Thanks in Advance.

try this::
this.getPackageManager().getApplicationInfo(this.getPackageName(), 0);

Result doesn't extend Context like your Activity class does. So the method isn't available in that class. You need to call act.getPackageManager() inside there instead of this.getPackageManager().

Related

Duplicate method to log out

I'm a beginner and I have (I think) a simple question for you. I have a method used in every Activity except LoginActivity. It's opened when I click on the shutdown icon. This is code ->
public void logOut(MenuItem item) {
Intent intent = new Intent("THIS_CLASS".this, LoginActivity.class);
startActivity(intent);
}
but I do not want to duplicate it in every Activity, may exist any solution ? I am writing here because I can't find a solution on the Web. The problem is that I can not express my intentions in the question on the Internet. Every "duplicate method" gives answers not on the subject. I am not looking for complete code. I prefer only prompt.
Firstly, create a help class and add a static method like this:
import android.content.Context;
import android.content.Intent;
public class MyHelper {
public static void startActivityB(Context context) {
Intent intent = new Intent(context, LoginActivity.class);
context.startActivity(intent);
}
}
Then call the method and pass correct context like below:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyHelper.startActivityB(YourActivity.this);
}
});
Note: if MyHelper, LoginActivity,YourActivity are not in a same package, you should import corresponding package, that is an easy job.
It's not the best solution, but if you want a bunch of activities to share the same methods, then create a BaseActivity that they all extend from.
public abstract class BaseActivity extends Activity {
//... Shared stuff
protected void logOut() {
//Do some data cleaning and whatever else you need
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
}
public class MyActivity extends BaseActivity {
//... Main code
private void onLogOutClicked() {
logout();
}
}
Just make sure you clean out your authentication/session data when this occurs. Try to utilise inheritance for shared functionality.

Call function from jar in MainActivity

First time I'm trying to make my own .jar file. It works, but now I want to give feedback to the MainActivity. So I want to call a function 'receiveSerial()' in the MainActivity.
So the MainActivity must always implement the function 'receiveSerial()' when including my .jar.
.jar file (part of the) code:
package com.hoeks.ma.bluetooth;
import java.util.Set;
import ...
public class Blauwe{
..
private Activity ma;
public Blauwe(Activity m){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
ma = (Activity)m;
}
..
public void sendSerial(String s) {
ma.receiveSerial(s); // This line give Eclipse error "Add cast to ma"
// When I add the cast it is not working
}
MainActivity
import com.hoeks.ma.bluetooth.Blauwe;
....
public void receiveSerial(String s) {
javascr.setSerial(s);
}
Note: I do not post the whole code because the code is a big mess right now, its not good for the readability.
1) create interface
public interface ReceiveSerialCallback{
public void receiveSerial(String s);
}
2) add interface implementation in MainActivity
public class MainActivity implements ReceiveSerialCallback{
...
public void receiveSerial(String s) {
// serial received
}
}
3) update Blauwe class
private ReceiveSerialCallback callback;
...
public void setReceiveSerialCallback(ReceiveSerialCallback callback) {
this.callback = callback;
}
...
public void sendSerial(String s) {
callback.receiveSerial(s);
}
4) set inteface callback object to Blauwe class in MainActivity
Blauwe b = new Blauwe();
b.setReceiveSerialCallback(this);
You have to cast because receiveSerial(String) is not a method of Activity, but MainActivity. I would create an interface (with method sendSerial) that MainActivity should implement, and save a reference of this interface in Blauwe class, instead of an Activity instance.

Call Public void

I have a public void in one class and I want to call it in another class when it creates but nothing seems to be working. here is the code of my first activity
public class activityone extends Activity {
public void actionC() {
//actions
}
Does anyone know how to call it in my second class?
In general, you need to have an instance of your activityone class in order to call an instance method.
To create an instance, you generally use a constructor like:
activityone a = new activityone();
a.actionC();
I'm not sure this is what you want though, because Activitys are generally created by the Android system itself and you should handle the onCreate method instead.
Here is what you can do:
public class activityone extends Activity {
/*public void actionC() {*/ //Instead on normal method, write your actions in onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//actions
}
and in your second activity, do this:
Intent intent = new Intent(getApplicationContext(),activityone.class);
startActivity(intent);
Hope it helps !!!

Trying to call an activity, but having troubles with onClick

im trying to run this simple code (im a beginner :).
trying to run this. the // text is usually what i use for buttons. however, i saw this switch technique that i wanted to try, it seemed more efficient. however, i get errors related to the onClick (something about the ( ) and that 'void is an invalid type'). i have no idea what can cause this. just wanna access the buttons. can anyone please tell me why?
Thanks!
package com.experiment.fewops;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FewOptions extends Activity {
/** Called when the activity is first created. */
final Button sexy = (Button) findViewById(R.id.buttonSexy);
final Button text = (Button) findViewById(R.id.buttonText);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// sexy.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v) {
// Intent intent = new Intent(this, SexyPage.class);
// startActivity(intent);
// }
// });
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonSexy:
Intent intent = new Intent(this,SexyPage.class);
startActivity(intent);
break;
}
};
}
}
There are actually 2 problems here:
First, as #Saiesh said, if you want to implement the click listener at the class level, you need to change your class declaration to implment OnClickListener. So your declaration will look like
public class FewOptions extends Activity implements OnClickListener{
The second problem (and the reason you're getting the error about void being a bad type) is that you're declaring the onClick method in the body of your onCreate method. move the declaration of the onClick method outside the closing brace (}) of the onCreate method and that error should go away.
One more note: after you make the 2 fixes above, don't forget to add your class as the click listener for the button:
sexy.setOnClickListener(this);
You need to pass right context
Intent intent = new Intent(FewOptions.this,SexyPage.class);
FewOptions.this.startActivity(intent);
Well the solution is that to use this onClick() method your class needs to implement the onClickListener interface . Thus your class heading should be something like this
public class FewOptions extends Activity implements onClickListener
{
//Eclipse will automatically ask you to override the onClick() method
}
So thats the solution :)

Problem in Using PackageManager in Android [duplicate]

I got a problem in one Android Application which I am working in which I am getting error when I use PackageManager.
The Application is as follows:
Home.java
package com.main.home;
public class Home extends Activity
{
//onclicking a Button say "send"
send.onClickListener()
{
public void onClick() {
Intent i =new Intent();
i.setClassName("com.main.home", "com.main.home.home1");
startActivity(i);
}
}
Home1.java
package com.main.home;
import com.andr.resulting.Result ;
public class Home1 extends Activity {
EditText et=(EditText)findViewById(R.id.e1);
//Clicking on forwardButton in that onClick()
public void onClick()
{
String s[] = {e1.getText().toString(),""};
//Calling a method in a class Result of another package which is not activity
Result.finalfunc(Home1.this,s);
}
Result.java
package com.andr.resulting; //different package
public class Result {
public static void finalfunc(Activity act,String[] re) ...
//Here I want to get the details of this particular class's package (com.andr.resulting) using PackageManager
// I tried like this:
Result.this.getPackageManager().getApplicationInfo(Result.getPackageName(),0))
I am getting error getPackageManager() does not exists in this class file.
How do I solve this issue? I will be eagerly waiting for valuable reply.
Thanks in Advance.
try this::
this.getPackageManager().getApplicationInfo(this.getPackageName(), 0);
Result doesn't extend Context like your Activity class does. So the method isn't available in that class. You need to call act.getPackageManager() inside there instead of this.getPackageManager().

Categories

Resources