0

我的应用程序崩溃了,但 logcat 没有显示任何内容。

我认为它在布局中,但是当我在 java 文件中禁用一些findViewById's 时,布局确实会加载..

我的布局:

<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>

这是我的 Java 文件:

包 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 确实显示:

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

在我尝试在布局上移动一些文本视图之前,一切都很好。

在我移动它们之后,它开始粉碎。我试图把我搬回来的所有东西都放回去,但没有帮助。

4

2 回答 2

1

在我尝试在布局上移动一些文本视图之前,一切都很好。

这始终是尝试清理项目的好时机。

“项目 --> 清理……”

有时 Eclipse 无法layout立即识别更改,您会收到类似这些没有意义的错误。据我所知,您正在声明它并正确初始化它,因此当您收到一条错误消息说您正在尝试将一个对象转换为另一个对象并且您知道自己不是时,请清理项目并且 Eclipse 应该正确检测到它。这通常发生在更改 xml 文件之后。

于 2013-08-11T17:10:36.540 回答
0

改变

ProgressBar Loadaddr;

TextView Loadaddr;
于 2013-08-11T17:04:45.510 回答