Android编程中的常见问题及解决办法

   在Android开发中新手通常会遇到各种各样调试不通过或者程序崩溃的问题,有些与环境配置有关、有些与代码编写有关。不管怎么说,有问题就要记录下来以便下次遇到同样的问题能够快速解决,同时也是对自己开发能力的一种提升。下面,本文将记录个人在开发中遇到的各类常见问题(不断更新......)

1. java.lang.NoClassDefFoundError

    这个问题出现在一次视频开发中。本人将先前开发过的一套完整Android代码复制到一个新的工程目录,将其中有关摄像机认证连接的代码做了整体替换之后重新编译并通过。但是在启动程序后从一个界面调转到另一个界面的时候出现崩溃,JVM抛出runtime异常,日志显示找不到加载类。

    解决办法:原来该class中需要加载另外的JAR包,然而在工程拷贝的过程中误将lib目录下的jar文件删除导致classloader加载出错。通过导入jar包后重新编译程序一切正常。

2. 去除Android程序自带的状态栏

    新建一个Android程序时,通常程序会带有一个状态栏。如果想要去除状态栏的话只需要在AndroidMainFest中设置相关的Application属性即可:

 <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >

        <activity
            android:name="iton.lifecute.activity.SplashScreen"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:windowSoftInputMode="stateHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

android:theme="@android:style/Theme.NoTitleBar" 表示程序的主题为无状态栏并且是对整个应用程序而言,如果只想为某个Activity设置为无状态栏只需要将改属性添加到对应的Activity声明中。需要说明的是Android系统自带的状态栏还是会显示的,如果需要的话可以将程序设置为全屏,只需要在改属性后加上

 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"即可。 
   

3. eclipse导入Android工程出现中文乱码

    有时候我们在用eclipse导入一个现有的Android工程时会出现中文字符乱码的现象,这个是因为你选择的workspace的编码方式与你已经存在的工程的编码方式不同,中文就会出现乱码从而可能引发程序报错。我们需要在eclipse中设置相应的编码方式即可。

3. Adapter开发中没有回调getView()的问题

 适配器(Adapter)是安卓开发中经常用到的一类工具,它为我们的后端数据与前端显示提供了良好的接口,常见的适配器view有:ListView、GridView等。今天在开发自定义Adapter的时候遇到了设置setAdapter()之后没有回调自定义的getView()方法来显示想要的界面。贴出代码如下:
class ListDeviceAdapter extends BaseAdapter
	{
		ChildListView childlist = null;
		private Context mContext;
		private List<Map<String, Object>> mDeviceData; // 设备信息集合

		public ListDeviceAdapter(Context context, List<Map<String, Object>> listItems)
		{
			this.mContext = context;
			this.mDeviceData = listItems;
			System.out.println("-- ListDeviceAdapter is running...--");
		}

		@Override
		public int getCount()
		{
			return 0;//return mDeviceData.size();//首先回调getCount()方法,根据其值来决定是否回调getView()
		}

		@Override
		public Object getItem(int position)
		{
			return mDeviceData.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			System.out.println("-- getView is running...--");
			if (convertView == null)
			{
				System.out.println("-- list is running...--");
				childlist = new ChildListView();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_control, null);
				childlist.mDeviceType = (ImageButton) convertView.findViewById(R.id.list_btn_device_type);
				childlist.mDeviceName = (TextView) convertView.findViewById(R.id.list_text_device_name);
				childlist.mDeviceControl = (ImageButton) convertView.findViewById(R.id.list_btn_device_type);
				convertView.setTag(childlist);
			} else
			{
				childlist = (ChildListView) convertView.getTag();
			}

			childlist.mDeviceType.setBackgroundResource(R.drawable.device_light);
			childlist.mDeviceName.setText(mDeviceData.get(0).get("title").toString());
			childlist.mDeviceControl.setBackgroundResource(R.drawable.device_curtain);

			childlist.mDeviceType.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{

				}
			});
			childlist.mDeviceControl.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{

				}
			});

			return convertView;
		}

		class ChildListView
		{
			ImageButton mDeviceType;
			TextView mDeviceName;
			ImageButton mDeviceControl;
		}
	}
经过反复检查发现自定义Adapter先是回调的getCount()方法,起初返回的是空,Adapter判断其返回值来决定是否回调getView方法。问题就在这里,简单修改其返回值,即返回传人的List数据大小,问题得到解决。

4. android.view.InflateException: ........Caused by: java.lang.StackOverflowError

11-09 15:46:04.410: E/AndroidRuntime(16591): FATAL EXCEPTION: main
11-09 15:46:04.410: E/AndroidRuntime(16591): java.lang.RuntimeException: Unable to start activity ComponentInfo{iton.lifecube/iton.lifecube.activity.MainActivity}: android.view.InflateException: Binary XML file line #38: Error inflating class <unknown>
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.os.Looper.loop(Looper.java:137)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at java.lang.reflect.Method.invoke(Method.java:511)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at dalvik.system.NativeStart.main(Native Method)
11-09 15:46:04.410: E/AndroidRuntime(16591): Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class <unknown>
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.createView(LayoutInflater.java:613)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.Activity.setContentView(Activity.java:1867)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at iton.lifecube.activity.MainActivity.onCreate(MainActivity.java:78)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.Activity.performCreate(Activity.java:5008)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	... 11 more
11-09 15:46:04.410: E/AndroidRuntime(16591): Caused by: java.lang.reflect.InvocationTargetException
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at java.lang.reflect.Constructor.constructNative(Native Method)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.view.LayoutInflater.createView(LayoutInflater.java:587)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	... 26 more
11-09 15:46:04.410: E/AndroidRuntime(16591): Caused by: java.lang.StackOverflowError
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.graphics.Bitmap.getScaledWidth(Bitmap.java:833)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.graphics.drawable.BitmapDrawable.computeBitmapSize(BitmapDrawable.java:179)
11-09 15:46:04.410: E/AndroidRuntime(16591): 	at android.graphics.drawable.BitmapDrawable.setBitmap(BitmapDrawable.java:187)

今天遇到一个很奇葩的问题,同样的一套代码在4.1和4.2的手机上都没问题,在4.1的平板上却报出了以上错误,百度了n多资料有各种原因并做了各种尝试,但就是没解决问题。伤心之余决定向google求助,果然众里寻他千百度、答案却在google中。原来是由于xml加载图片的时候在自定义Drawable的xml文件中和添加的图片资源中有重名的,导致了以上错误。修改即可,下次在自定义Drawable资源文件时一定要记得规范命名哦,最好加上后缀:_selecter。


你可能感兴趣的:(Android编程中的常见问题及解决办法)