当前位置: 代码迷 >> Android >> android开发常见有关问题集
  详细解决方案

android开发常见有关问题集

热度:81   发布时间:2016-05-01 13:21:09.0
android开发常见问题集

转载声明:本文出自http://androidkaifa.com/thread-960-1-1.html


  • www.androidkaifa.com提示:Issue意思: 问题,Solution意思:解决方案
  • Development
    • [Issue] Application crush when return from predefined interface
      [Solution 1] You may forgot to call the method of predefined super class
      i.e.: In your Activity, if you override onDestroy as following:
      class YourActivity extends Activity
      {
          protected void onDestroy()
          {
          }
      }
      when the activity is destroyed, application will crash, in order to resolve this issue, you must modify the code snip to:
      class YourActivity extends Activity
      {
          protected void onDestroy()
          {
              super.onDestroy();  //Call the same method of super class
          }
      }
    • [Issue] Explain view's attributes
         android:gravity: Define which direction to align control's contents.
    • [Issue] How to get and Set system property
      [Solution] Use methods System.getProperty(xxx) and System.setProperty(xxx), following properties can be got and set:
              java.vendor.url
              java.class.path
              user.home
              java.class.version
              os.version
              java.vendor
              user.dir
              user.timezone
              path.separator
              os.name
              os.arch
              line.separator
              file.separator
              user.name
              java.version
              java.home
    • [Issue] How to get popular directories and their stat
      [Sulotion] Call method android.os.Environment, following directories can be received:
         a) root directory
         b) external storage directory
         c) data directory
         d) download cache directory
         e) external storage state,  
      Note: USB mass storage path is /sdcard
    • [Issue] How to access resource in package in Acrivity
      - Get resource value
         getResources().getString(resID);  //resID looks like R.string.str_id
      - Get resource id from name
         getResources().getIdentifier("name", "string", getPackageName());
         getResources().getIdentifier("name", "drawable", getPackageName());
    • [Issue] How to get and  set environment variables
      [Solution] To get the value of the specified environment variables, just use System.getenv(xxx); But you can't set environment variable in Java code directly, you need to use jni to call C function 'setenv'.
    • [Issue] How to know byte order of device
      [Solution] Call method ByteOrder.nativeOrder();
    • [Issue] How to read files in the directories res/raw
      [Sulotion]
      Resources res = getResources();
      InputStream is = res.openRawResource(R.raw.xxx);
    • [Issue] How to display horizontal bar in widget
      [Solution] Use a view which background is filled with the specified color.
      <TableLayout xxxxxx>
          <TableRow xxxxxx>
             ......
          </TableRow>
          ......
          <View android:layout_height="2dip"
              android:background="#FF909090" />
          ......
      </TableLayout>

      Comments:
      1) A control View with the specified background is shown as a bar
      2) The View must be declared within TableLayout
      3)If your want to implement the horizontal bar with left/right edgesfading just like ListView's divider, you only need to setandroid:background to "?android:attr/listDivider"
    • [Issue] How to change contents of TextView
      - Set text view to be editable
          TextView textView = findViewById(xxx);
          textView.setText(textView.getText(), TextView.BufferType.EDITABLE);
      - Change contents
          textView.setText(xxx);
          textView.append(xxx);
    • [Issue] Customize View
      - Define customized view class as following
          package com.test.customized;
          import xxxxxx;
          class CustomizedTextView
          {
              private static String HEADER = "(Warning)";
              private static String RETURN = "\n";


              public TextView(Context context)
              {
                  super(context);
              }
         
              public TextView(Context context, AttributeSet attrs)
              {
                  super(context, attrs);
              }

              public TextView(Context context, AttributeSet attrs, int     defStyle)
              {
                  super(context, attrs, defStyle);
              }


              public void append(CharSequence text, int start, int end)
              {
                  super.append(HEADER, 0, HEADERS.length());
                  super.append(text, start, end);
                  super.append(RETURN, 0, RETURN.length());
              }
          }

      - Use the customized view as following
          <?xml version="1.0" encoding="utf-8"?>
          <com.test.customized.CustomizedTextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Hello, Android"/>
    • [Issue] How to use multiple Activity
      [Solution] Please refer to:
      http://www.apkcn.com/android-80860-1-1.html
      http://blog.csdn.net/Class_Raito/archive/2008/11/27/3390737.aspx
    • [Issue] How to launch activity
      [Solution]
      Launch application
      1) From shell
        usage: am [start|instrument]
             am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                      [-c <CATEGORY> [-c <CATEGORY>] ...]
                      [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                      [-n <COMPONENT>] [-D] [<URI>]
             am instrument [-e <ARG_NAME> <ARG_VALUE>] [-p <ROF_FILE>]
                      [-w] <COMPONENT>
              
      For example, launch an application whose manifest are:
              
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.xiashou.android">   
          <application>
              <activity>
                <intent-filter>  
                  <categoryandroid:name="android.intent.category.LAUNCHER" />   
                </intent-filter>
              </activity>
          </application>
      </manifest>
              
      Command is: #am start -n net.xiashou.android/net.xiashou.android.HelloAndroid      

      Other usage:
      a) Launch browser to open an URI: #am start -a android.intent.action.VIEW -d http://www.xiashou.net
      b) Call out: #am start -a android.intent.action.CALL -d tel:10086
      c) Launch google map to locate TianJing: #am start -a android.intent.action.VIEWgeo:0,0?q=tianjin

      2) From on activate:
      Use Intent.
      For example, launch an activate whose manifest is:
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="koushikdutta.superuser" android:versionCode="1"
          android:versionName="1.0.0">

          <permission android:name="android.permission.ACCESS_SUPERUSER"
              android:label="@string/superuser_permissions_name"
              android:description="@string/superuser_permissions_description"
              android:protectionLevel="dangerous" />

          <application android:icon="@drawable/icon" android:label="@string/app_name"
              android:debuggable="true">
              <activity android:name=".SuperuserActivity" android:label="Superuser Whitelist">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
              <activity android:name=".SuperuserRequestActivity"
                  android:label="Superuser" android:permission="android.permission.ACCESS_SUPERUSER">
                  <intent-filter>
                      <action android:name="android.intent.action.superuser" />
                      <category android:name="android.intent.category.DEFAULT" />
                  </intent-filter>
              </activity>
          </application>
      </manifest>


      Launch code is:
      Intent intent = new Intent("android.intent.action.superuser");
      startActivityForResult(intent, SUPERUSER_REQUEST);
    • [Issue] How to close current Activity and remove it from Activityhistory stack when you switch to other Activity
      [Solution] Call"finish()" in current Activity's methods.
    • [Issue] How to use class "Cursor"
      [Solution] Cursor stores the results of database query, such asContentResolver.query(xxxxx), Cursor contains M(Rows) x N (Columns) eluments,each column has its name and value, we can get them by callinggetColumnName() and getString/getInt()/getLong()/getShort()..... etcrespectively.
    • [Issue] How to send Http request
      [Solution] Use folloing code:
      (refer to
      http://blog.atrexis.com/index.cfm/2008/6/20/Sending-HTTP-requests-in-Android
      http://dotphone.org/redirect.php?tid=33&goto=lastpost
      http://dev.shufou.com/?p=28

      )

          URL url = new URL("www.baidu.com");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setDoInput(true);
          conn.setConnectTimeout(10000);
          conn.setRequestMethod(HttpURLConnection.GET);
          conn.setRequestProperty("accept", "*/*");
          String location = conn.getRequestProperty("location");
          int resCode = conn.getResponseCode();
          InputStream stream = conn.getInputStream();
    • [Issue] Java Network exception
      - When establish network connection using socket, SocketException isthrowed, and exception readable string is: "java.net.SocketException:Connection reset".
      Solution: add android.permission.INTERNET to application manifest
      - Android java.net.SocketException: The connection was reset
      Reason: The time out value is too long, try 10 seconds.
文章过长,请点击此处查看全http://androidkaifa.com/thread-960-1-1.html

  相关解决方案