应用程序崩溃可能是布局错误。我找不到它
Posted
技术标签:
【中文标题】应用程序崩溃可能是布局错误。我找不到它【英文标题】:Application crashing probably layout error. I cant find it 【发布时间】:2013-08-11 16:37:58 【问题描述】:我的应用程序崩溃了,但 logcat 没有显示任何内容。
我认为它在布局中,但是当我在 java 文件上禁用一些 findViewById
时,布局确实会加载..
我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
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_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="136dp"
android:layout_marginTop="205dp" />
<TextView
android:id="@+id/title"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/desc"
android:layout_
android:layout_
android:text="Item Description" />
<TextView
android:id="@+id/pub"
android:layout_
android:layout_
android:layout_marginTop="50dp"
android:text="Publisher" />
</LinearLayout>
</ScrollView>
<ProgressBar
android:id="@+id/loadimg"
android:layout_
android:layout_
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_
android:layout_
android:layout_alignBottom="@+id/addr"
android:layout_alignLeft="@+id/loadimg"
android:layout_marginLeft="15dp" />
<TextView
android:id="@+id/addr"
android:layout_
android:layout_
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 ComponentInfocom.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
在我尝试移动布局上的一些文本视图之前,一切都很好。
在我移动它们之后,它开始粉碎。我试图把我搬回来的所有东西都放回去,但没有帮助。
【问题讨论】:
"Logcat 不显示任何内容" - Logcat 总是打印堆栈跟踪。要么您需要将 Eclipse 中的 Logcat 消息过滤为仅来自您的应用程序的消息,要么您的过滤器过于严格。但我确信其中有一个堆栈跟踪将指向崩溃的原因。 这是非常多的代码。没有 Logcat 就很难找到问题所在。在运行应用程序之前尝试打开 logcat。然后发布日志猫 毕竟它确实显示了 logcat。抱歉,我不知道发生了什么,但我上次尝试时没有。那么我的代码有什么问题?我再次检查.. textViews 和 progressBars 似乎一切正常.. 检查你所有的 textviews 和 profressbar ids,有一些冲突。并清理您的项目并重试。 【参考方案1】:在我尝试在布局上移动一些文本视图之前一切都很好。
这始终是尝试清理项目的好时机。
“项目 --> 清理...”
有时 Eclipse 无法立即识别 layout
更改,您会收到类似这些没有意义的错误。据我所知,您正在声明它并正确初始化它,因此当您收到一条错误消息说您正在尝试将一个对象转换为另一个对象并且您知道自己不是时,请清理项目并且 Eclipse 应该正确检测到它。这通常发生在更改 xml 文件之后。
【讨论】:
是的。就是这样。这样一个愚蠢的问题..抱歉浪费你的时间。泰。 没问题。你没有浪费我的时间。它发生了很多。如此之多,以至于如果您在 SO 中搜索 ClassCastException,您可能会找到它;)但希望该解释对您将来有所帮助。【参考方案2】:改变
ProgressBar Loadaddr;
与
TextView Loadaddr;
【讨论】:
你能告诉我哪一个是第 61 行吗? 它的Loadimg =(ProgressBar)findViewById(R.id.loadimg);
但问题已解决.. 只是清洁项目。以上是关于应用程序崩溃可能是布局错误。我找不到它的主要内容,如果未能解决你的问题,请参考以下文章
那个NSIS ERROR的安装程序在哪 我找不到 我的NSIS ERROR错误不是因为网游的关系