mn6vdv23g
mn6vdv23g

Reputation: 734

Application crashing probably layout error. I cant find it

My app is crushing but the logcat doesnt show anything.

I think its something in the layout, but the layout does load when I disable some findViewById's on the java file..

My layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ItemView" >

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="136dp"
        android:layout_marginTop="205dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="191dp"
        android:layout_marginRight="156dp"
        android:text="Item Title"
        android:background="#DCFFFF"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/title"
        android:layout_alignLeft="@+id/title"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/title"
        android:layout_marginLeft="140dp"
        android:background="#d5d4d4"
        android:src="@android:drawable/gallery_thumb" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/imageView1"
        android:layout_below="@+id/loadaddr"
        android:background="#ADF3F2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="17dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Item Description" />

            <TextView
                android:id="@+id/pub"
                android:layout_width="123dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:text="Publisher" />
        </LinearLayout>
    </ScrollView>

    <ProgressBar
        android:id="@+id/loadimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="94dp"
        android:layout_marginLeft="48dp" />

    <ProgressBar
        android:id="@+id/loadaddr"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/addr"
        android:layout_alignLeft="@+id/loadimg"
        android:layout_marginLeft="15dp" />

    <TextView
        android:id="@+id/addr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/title"
        android:layout_below="@+id/imageView1"
        android:gravity="center"
        android:text="Fetching Address" />

</RelativeLayout>

And this is my Java file:

package com.example.free;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.free.MainActivity.Load;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ItemView extends Activity {
    public void ToastLoadShout(String msg){Toast.makeText(this, msg.toString(), Toast.LENGTH_LONG).show();}
    item item;
    ProgressBar Loadaddr;
    ProgressBar Loadimg;

    String addrString="";
    String requestUrl;
    String downloadUrl;

    String lat,lon;
    int id;

    TextView title;
    TextView addr;
    TextView desc;
    TextView pub;

    ImageView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_view);

        /*title = (TextView)findViewById(R.id.title);
        Loadaddr =(ProgressBar)findViewById(R.id.loadaddr);
        Loadimg =(ProgressBar)findViewById(R.id.loadimg);
        addr = (TextView)findViewById(R.id.addr);
        desc = (TextView)findViewById(R.id.desc);
        pub = (TextView)findViewById(R.id.pub);*/
        view= (ImageView)findViewById(R.id.imageView1);

        item = getIntent().getParcelableExtra("thing");

        lat=item.getLat()+"";
        lon=item.getLon()+"";

        /*title.setText(item.getTitle());
        desc.setText(item.getDesc());
        pub.setText(item.getPub());*/
        id=item.getId();

        //new Load().execute();
        downloadUrl="http://apple.sourcherry.tk/freeapp/img/"+id+".jpg";
        new ImageDownloader().execute(downloadUrl);

    }

    public String GetAddress(String lat, String lon)
    {
        Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
        String ret = "";
        try {
            List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(lat), Double.parseDouble(lon), 1);
            if(addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("");
                for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                ret = strReturnedAddress.toString();
            }
            else{
                ret = "Unknown Address";
            }
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = "Can't get Address";
        }
        return ret;
    }

    class Load extends AsyncTask<String, Integer, Boolean>
    {
        @Override
        protected void onPreExecute() {

        }
        @Override
        protected Boolean doInBackground(String... params) {

            try {
                addrString = GetAddress(lat,lon);
                if (addrString!="")
                    return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return false;
        }
        @Override
        protected void onPostExecute(Boolean res) {
            // TODO Auto-generated method stub
            if (res){
            addr.setText(addrString);
            Loadaddr.setVisibility(View.INVISIBLE);
            }
            else
                addr.setText("Unable to fetch address.");
    }




}
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.item_view, menu);
        return true;
    }

    private class ImageDownloader extends AsyncTask<String,String,Bitmap> {

        protected Bitmap doInBackground(String... param) {
            // TODO Auto-generated method stub
            return downloadBitmap(param[0]);
        }

        protected void onPreExecute(String res) {
            Log.i("Async-Example", "onPreExecute Called");

        }
        protected void onPostExecute(Bitmap result) {
            Log.i("Async-Example", "onPostExecute Called");
            view.setImageBitmap(result);
            Loadimg.setVisibility(View.INVISIBLE);

        }

        private Bitmap downloadBitmap(String url) {
            // initilize the default HTTP client object
            final DefaultHttpClient client = new DefaultHttpClient();

            //forming a HttoGet request 
            final HttpGet getRequest = new HttpGet(url);
            try {

                HttpResponse response = client.execute(getRequest);

                //check 200 OK for success
                final int statusCode = response.getStatusLine().getStatusCode();

                if (statusCode != HttpStatus.SC_OK) {
                    Log.w("ImageDownloader", "Error " + statusCode + 
                            " while retrieving bitmap from " + url);
                    return null;

                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        // getting contents from the stream 
                        inputStream = entity.getContent();

                        // decoding stream data back into image Bitmap that android understands
                        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                        return bitmap;
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            } catch (Exception e) {
                // You Could provide a more explicit error message for IOException
                getRequest.abort();
                Log.e("ImageDownloader", "Something went wrong while" +
                        " retrieving bitmap from " + url + e.toString());
            } 

            return null;
        }

    }
}

Logcat Does show after all:

08-11 19:54:45.437: E/AndroidRuntime(1218): FATAL EXCEPTION: main
08-11 19:54:45.437: E/AndroidRuntime(1218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.ItemView}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ProgressBar
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.os.Looper.loop(Looper.java:137)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread.main(ActivityThread.java:5227)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at java.lang.reflect.Method.invoke(Method.java:511)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at dalvik.system.NativeStart.main(Native Method)
08-11 19:54:45.437: E/AndroidRuntime(1218): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ProgressBar
08-11 19:54:45.437: E/AndroidRuntime(1218):     at com.example.free.ItemView.onCreate(ItemView.java:61)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.Activity.performCreate(Activity.java:5104)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-11 19:54:45.437: E/AndroidRuntime(1218):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-11 19:54:45.437: E/AndroidRuntime(1218):     ... 11 more

Everything was fine before I tried to move some of the textviews on the layout.

After I moved them, it began to crush. I tried to put everything I moved back but it didnt help.

Upvotes: 0

Views: 231

Answers (2)

codeMagic
codeMagic

Reputation: 44571

Everything was fine before I tried to move some of the textviews on the layout.

This is always a good time to try cleaning your project.

"Project --> Clean..."

Sometimes Eclipse doesn't recognize layout changes right away and you will get errors like these that don't make sense. From what I see, you are declaring it and initializing it properly so when you get an error message saying you are trying to cast one object to another and you know you aren't, clean the project and Eclipse should detect it properly. This often happens after changing xml files.

Upvotes: 1

Stefano Munarini
Stefano Munarini

Reputation: 2717

Change

ProgressBar Loadaddr;

with

TextView Loadaddr;

Upvotes: 0

Related Questions