I am using gradient as background in my activity. on some android devices it doesn't look as good and smooth as in Photoshop, to fix this issue somebody told me use onAttachedToWindow() method.
I checked Android page (http://developer.android.com/reference/android/app/Activity.html#onAttachedToWindow()) and I found that this method is a part of android.app.Activityand I wrote following lines of code:
package com.test.test1;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
public class Mainctivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
}
but when run the emulator, it crashed and in DDMS I saw this error:
11-25 10:48:13.353: E/dalvikvm(216): Could not find method android.app.Activity.onAttachedToWindow, referenced from method com.test.test1.MainActivity.onAttachedToWindow
What is my fault?
This method is available since API Level 5. What version of Android is running on the emulator?
As per the comments above, I've had this code tested on an actual device and it worked smoothly. So this is an emulator problem. Hopefully this will be resolved in later versions of the sdk.
Related
I have an Android app build with the newest version of Android Studio.
I want to allow only portrait mode on phone but all orientations on tablets.
I followed this answer and also this post.
I made everything as described.
Then I opened the file activity/MainActivity.java.
I looked for this line of code: public void onCreate(Bundle savedInstanceState) {
Then I added the following code below this code:
if(getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Here's a small snippet:
#Override
public void onCreate(Bundle savedInstanceState) {
if(getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityInfo has a red color with the following error message:
Cannot resolve symbol 'ActivityInfo'
It also shows a blue information:
android.content.pm.ActivityInfo? ⌥⏎
Why that? What am I doing wrong?
You need to add the following line at the top of your file below package name to import ActivityInfo:
import android.content.pm.ActivityInfo;
I am pretty new to android programming. I was trying to implement a app which pulls all android sensor values and displays on screen. I referred some online documents and tried writing below.
package com.example.my.robuto;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.List;
public class phone_sensor extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_sensor);
SensorManager sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
//Sensor proximity_sensor = sMgr.getDefaultSensor(Sensor.);
List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL);
}
}
When i type type the last part sMgr.getSensorList(Sensor.TYPE_ALL) I see TYPE_ALL shows up in RED. Also if i look at options for Sensor. I don't see anything with TYPE_ ! Am i missing something? I selected API 25: Android 7.1.1 (Nougat) in android studio.
It was silly of me to not use: import android.hardware.Sensor;
Once i use it issue is fixed.
I'm working on a phonegap project which contains a canvas overlayed with kinetic.js, which allows a user to pinch zoom and pan around an image, then draw annotations on it. it works spliendidly in a browser and on windows and apple tablets, but of course android is a good bit slower.
as a solution, i've released the app using https://github.com/thedracle/cordova-android-chromeview. after switching my main java class to use ChromeView as the webview, i'm getting this error on startup:
12-03 13:21:09.083: E/chromium(13917): [ERROR:aw_browser_context.cc(191)] Not implemented reached in virtual quota::SpecialStoragePolicy* android_webview::AwBrowserContext::GetSpecialStoragePolicy()
after debugging through the codebase, it looks like the error is triggering here:
private void setNativeContentsClientBridge(int nativeContentsClientBridge) {
mNativeContentsClientBridge = nativeContentsClientBridge;
}
(AwContentsClientBridge.java line 36).
i'm trying to find out what the nativeContentsClientBridge int is. My value is 1611312352 but i haven't a notion of what that represents.
my gut feel is that the chromium browser is missing an implementation for accessing localstorage. i found this bug:
https://github.com/pwnall/chromeview/issues/27
where someone is experiencing the same thing, but there is no solution.
for assistance, this is my main activity class:
package com.companion;
import org.apache.cordova.Config;
import org.apache.cordova.CordovaActivity;
import us.costan.chrome.ChromeSettings;
import us.costan.chrome.ChromeView;
import android.os.Bundle;
public class CompanionApp extends CordovaActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
ChromeView chromeView = new ChromeView(CompanionApp.this);
ChromeSettings settings = chromeView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
setContentView(chromeView);
super.loadUrl(Config.getStartUrl());
}
}
Thanks for your help,
Margaret
Don't know if it's normal, but it seems that a method's not implemented in Chromium base code : https://github.com/01org/pa-chromium/blob/master/android_webview/browser/aw_browser_context.cc
Here's that particular method:
quota::SpecialStoragePolicy* AwBrowserContext::GetSpecialStoragePolicy() {
// TODO(boliu): Implement this so we are not relying on default behavior.
NOTIMPLEMENTED();
return NULL;
}
I hope it helps :)
I have developed Android applicationusing Phonegap framework with Eclipse pulgin. am not gettting any error in Eclipse but when i'm running the app am able to see the blank app screen.So can someone help me out this issue..
Sample app developed using this link
Please comment asap..
Code:
Java File ----------
package com.phonegap;
import com.phonegap.*;
import android.os.Bundle;
public class HelloworldActivity extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_assets/www/helloworld.html");
}
}
The problem is in your loadUrl, you should use:
super.loadUrl("file:///android_asset/index.html"); // Singular android_asset, not assets
In Ice Cream Sandwich, when there's an Activity containing an EditText, the EditText will retain the Activity's Context even after the user leaves the Activity. To demonstrate this I've created TestLeakActivity, which allocates a large byte array. Since the Activity's Context is never garbage collected, the byte arrays accumulate on the heap, eventually causing an OutOfMemoryError. You can observe the heap growth by using the DDMS heap tool, and you can track the outstanding references to the EditText class by looking at the HPROF file in Eclipse MAT. To create memory leaks, go into LaunchActivity and just keep launching and backing out of TestLeakActivity.
LaunchActivity.java
package com.example.testleakproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LaunchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Start TestLeakActivity");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LaunchActivity.this, TestLeakActivity.class);
startActivity(intent);
}
});
ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
container.addView(button);
}
}
TestLeakActivity.java
package com.example.testleakproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;
public class TestLeakActivity extends Activity {
private byte[] mSomeBytes = new byte[1048576];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText editText = new EditText(this);
editText.setHint("TestLeakActivity");
ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
container.addView(editText);
}
}
This is a known bug, that will be fixed in ICS MR1.
This has not been fixed until now. (Android 4.2.1)
I've just spend several hours to find that I'm affected by this issue.
The issue seems to be caused by the spell checker. When I disable suggestions for the EditText view everything is properly garbage collected.
mInputType = mText.getInputType();
mText.setInputType(mInputType | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
I don't really want to disable this, since many users want spell checking. So, maybe there is a way to temporarily enable it when the input field receives the focus.
If you don't need the spell checker just add this to the EditText element in your layout xml instead:
android:inputType="textNoSuggestions"
That seems to fix it too.
Edit:
Just found this thread that appears to be related: Work around SpellCheckerSession leak?
I'm experiencing the same. My Gingerbread devices all work fine, but testing on my Galaxy Nexus this situation arises predictably. What your experiencing is likely why the MR1 and 4.0.3 updates rolled out so quickly.
You are running into the situation described in the Android resources section on memory leaks. See that page for some solutions as well.
I got the same problem,
I solved it by hiding the EditText ondismiss of my dialog.
mEditText.setVisibility(View.GONE);