Picasso Main Errored - android

I am trying to use Picasso Android library but i'm not able to get it working.
For starting, i am trying the simplest of the things:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_view);
mImageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).setIndicatorsEnabled(true);
Picasso.with(this).setLoggingEnabled(true);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(mImageView);
}
the activity layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"/>
</RelativeLayout>
The view remains completely red.
If i set a resource in the view via setImageResource, it correctly displays.
The only relevant clue that something went wrong is a message saying:
Picasso: main errored
but i can't understand why.
I would happily paste more logcat output but it breaks the code block and becomes unreadable.
Here's a pastebin: main errored log
I believe there's something trivial i am doing wrong.

To download images from the internet, you will need the INTERNET permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />

Related

Can Android layout be loaded in the C language?

This link says that to make the app display a layout, you create a main_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
and then load it in onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// main_layout.xml is compiled into the R.layout.main_layout object
setContentView(R.layout.main_layout);
}
My problem is that I am coding in C, and have the C version of "onCreate" method:
JNIEXPORT
void ANativeActivity_onCreate(ANativeActivity *activity, void *savedState,
size_t savedStateSize) {
....
}
is it possible to load the layout and make the app display it in the C language?
C uses "ANativeActivity" that it doesn't has any of Java methods and resources/objects. Even if you manually parse that XML you should MANUALLY implement all Widgets/Components in C.....so it's near impossible due to high amount of work involved in it.
Usually someone chooses C on Android to do "some special work" that is not available on normal Java or due to performance issues on it.

setVisibility(View.GONE) causes a Crash

[Edit: I have just replaced adView.setVisibility(View.GONE) with adView.setVisibility(View.INVISIBLE) the WebView loads and nothing crashes, so it really looks like it is something to do with when I remove the AdView xml element, rather than just making it invisible. Making it invisible is not ideal, as you get a white empty bar at the bottom where the Ad should have been. So it really looks like it is something to do with reloading the Webview or messing with the UI. My html/javascript code is solid and can handle any dimension changes.]
I have a Webview above a banner for an advert (the "ca-app-pub-3940256099942544/6300978111" is the test ad id, so I am not given out any personal info)
<WebView
android:id="#+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#+id/adView" />
<!-- "BANNER" or "LARGE_BANNER" -->
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
Note: the app:layout_constrain... in the Webview. I think that might be part of the problem.
At start up, I am checking for purchases. If the user has made any purchases whatsoever, I turn off the ads with the code:
public void turnAdvertsOff() {
advertsOn = false;
AdView adView = (AdView) m_Context.findViewById(R.id.adView);
adView.destroy();
adView.setVisibility(View.GONE);
}
With the line adView.setVisibility(View.GONE); the program crashes with the unfounded allegation:
I/chromium: [INFO:CONSOLE(6381)] "Uncaught Error: Java exception was raised during method invocation", source: file:///android_asset/www/index.html?IsAndroidWebview=true (6381)
D/WebView: loadUrl=about:blank
D/WebView: destroy
However, I know there is nothing wrong in the Webview, as when I hash out the line //adView.setVisibility(View.GONE);, the WebView loads fine.
Does anyone know why?
Is it anything to do with the app:layout_constraint.., and if so how do I overcome it?
This is what is recommended to hide the adView:
adView.pause();
adView.setVisibility(View.GONE);
Try using a LinearLayout as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1" />
<!-- "BANNER" or "LARGE_BANNER" -->
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:adSize="BANNER"
android:layout_gravity="center"
app:adUnitId="ca-app-pub-3940256099942544/6300978111" />
</LinearLayout>
SMART_BANNER
OK, I have found the answer, and it is not obvious. It is a combination of #mTak 's answer and a critical bit of how Android apps work (which is something I did not realise since I come from an html/javascript world and multi-threading is something programmers have absolute control over in that environment).
The purchases checks were being made on another thread. So the attempt at doing adView.setVisibility(View.GONE) was causing grief to the program. It does not seem to like you messing with the UI except on the main UI thread.
So this is how I changed my code to make the Android protocols happy:
My Purchase check (in a separate thread, but in the MainActivity). Note, stringListARR is just an array of strings with the product codes that the user had purchased. So if there was at least one purchase, whatever it was, I had decided to turn off adverts:
// Any purchase means we have no adverts
myAdverts.advertsOn = stringListARR.size() > 0 ? false : true;
// This is the evil line of code that caused the problem - this was being called not on the UI thread
//if(!myAdverts.advertsOn) myAdverts.turnAdvertsOff();
// And this is how to do it properly forcing it to be run on the UI thread
// 'this' in the following is my MainActivity
if(!myAdverts.advertsOn){
this.runOnUiThread(new Runnable(){
#Override
public void run() {
myAdverts.turnAdvertsOff();
}
});
}
And #mTak was perfectly right how to do things properly. My turnAdvertsOff() looks like this:
public void turnAdvertsOff() {
advertsOn = false;
AdView adView = (AdView) m_Context.findViewById(R.id.adView);
adView.pause();
adView.setEnabled(false);
adView.setVisibility(View.GONE);
}
So thanks to #mTak and thanks to this almost unfindable thread on google which gave me the idea:
https://groups.google.com/forum/#!topic/google-admob-ads-sdk/d30EAC1zGFo
In fact, if any people here on Stackoverflow are answering questions about crashes when people are messing with the UI in Android, this runOnUiThread might be the first solution that springs to mind. It is not obvious and a pain to discover.

Automatically Generating java code for xml layout in Eclipse

I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link

Error inflating class TouchImageView

I'm trying to spruce up my image viewer activity by converting from an ImageView to a TouchImageView, per the answer here: How can I get zoom functionality for images?
However, I get an error when my activity is attempting to load:
Binary XML file line #15: Error inflating class TouchImageView
Here's my image_viewer.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
-->
<TouchImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
And my code inside onCreate of ImageViewer.java (though I'm pretty sure that execution never even gets this far):
if(filepath != null && filepath.length() > 0 && new File(filepath).exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(filepath);
TouchImageView imgView = (TouchImageView) findViewById(R.id.imageView1);
imgView.setImageBitmap(myBitmap);
}
Any ideas as to why or what is causing this error? What I saw was the inner most exception/cause. Not sure how I can get more details about the error.
Thanks in advance.
UPDATE:
Exception gets thrown on execution of this line:
setContentView(R.layout.image_viewer);
Further detail on the error:
java.lang.ClassNotFoundException: android.view.TouchImageView in
loader dalvik.system.PathClassLoader#44bfde70
You need to have the fully qualified name to any custom view classes. e.g:
<com.kon.thing.TouchImageView ... />
The JVM can't find your class.
make sure u have entered a correct package name in xml
like..
<com.gallery.TouchImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />

Issue with displaying TabHost Layout in new Intent

I am having an issue with using TabHost in a new Intent of type TabActivity which I hope you can point me in the right direction. Funnily it works fine when I try to view it in the original Intent : setContentView(R.layout.main)
I get a "forced closed" and within logcat, I get the following error even though my Tabhost id = "#android:id/tabhost":
02-18 22:23:11.937:
ERROR/AndroidRuntime(5944): Caused by:
java.lang.RuntimeException: Your
content must have a TabHost whose id
attribute is 'android.R.id.tabhost'
I have declared the second intent in the Manifest.xml file:
XML:
<activity android:name=".NextActivity" android:label="#string/app_name" >
Within the first activity (MainActivity), I start the second intent (NextActivity), with extras, as follows:
Intent nextActivity = new Intent(MainActivity.this,NextActivity.class);
Bundle b_next=new Bundle();
b_next.putString("s_string", myString);
nextActivity.putExtras(b_next);
In my NextActivity.java file, I get the extras and try to display the TabHost View:
public class NextActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String myString;
Bundle b_initial;
b_initial = getIntent().getExtras();
myString = b_initial.getString("s_string");
setContentView(R.layout.main);
}
}
I get the same error with using the TabHost example on the Android Developer site (Hellow View):
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="#+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
Thanks in advance folks...
CLARIFICATION:
This is what I really get from LogCat:
java.lang.NullPointerException
at android.widget.TabHost.dispatchWindowFocusChanged(TabHost.java 285)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640)
at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java 640)
at android.view.ViewRoot.handleMessage(ViewRoot.java 1645)
at android.os.Handler.dispatchMessage(Handler.java 99)
at android.os.Looper.loop(Looper.java 123)
at android.app.ActivityThread.main(ActivityThread.java 3948)
at java.lang.reflect.Method.invokeNative(Native
Method)
at java.lang.reflect.Method.invoke(Method.java 521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java 782)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java 540)
at dalvik.system.NativeStart.main(Native
Method)
I had the same problem. Luckily there was a quick fix:
Delete the R file.
Wait for your IDE to generate the file.
Profit!
I had copied and pasted code and noticed that in the id attribute I had:
android:id="#+id/tabs"
when what I really wanted was:
android:id="#android:id/tabs"
I have previously constructed tabhosts with an id of android:id="#+id/tabhost". Does this work for you?
You could also consider constructing your tab view programmatically:
TabHost t = getTabHost();
TabSpec tab = t.newTabSpec(label)
.setIndicator(label, icon)
.setContent(intent);
t.addTab(tab);
I also encountered this problem. I had an XML file that wasn't linked against my code properly in my res/layout. When I fixed the naming, the problem went away.
even though i had changed my main.xml to use android:id="#android:id/tabhost" like the error stated, it kept trying to use the old name. finally i found the file res/layout-port/main.xml that was identical to main.xml except used the wrong android:id. i must have added a portrait view accidentally and it was holding on to the old value. either fixing the portrait file or removing it fixes my problem.
I also had this error. For me it would appear after obfuscation(via ProGuard), pre obfuscation it was fine.
In the end I renamed my layout file to something more complex and unique, and ProGuard didn't mess up the linking. Bit easer than going down the ProGuard configeration path.
So my issue was similar to SapphireSun, though expressed via a different process(and post eclipse, just to make things that bit more difficult).
This is perhaps one of the more obscure ways to get this error, but I thought it worth a mention.
"public class NextActivity extends TabActivity" --change TabActivity to ActivityGroup.
when problem happen when you want wo run tabhost in tabhost.it is ok.
I am experiencing the same error in a team-project, when using SVN. Sometimes, eclipse uploads classes.dex and resources.ap_ from the bin folder of the project and others get it via SVN update.
Since these binary files are generated on the individual machines with individual code, thats probably where the error comes from.
Cleaning the project files (Project -> Clean) always solved the issue for us!
I had this issue along with 'ERROR: Unknown option '--no-crunch''. If you updated ADT plug-in in Eclipse but have not updated the SDK at the same time. Updating the SDK Manager in Eclipse, fixed the issue for me.

Categories

Resources