解决方案 »

  1.   

    Activity一般是用于显示UI的,不要用new Activity().getContacts().最好是写一个工具类,接受 Context。这样用起来方便。
      

  2.   

    就是你写的公用方法别放在Activity里就行了,随便放在其他的普通的类里面。public class Utils{  public XXX static getContacts(Context context){
      //操作。。
       return null;} }
      

  3.   

    但是我写的这个方法需要集成activity类啊怎么办呢
      

  4.   


    public class SIMCardInfo { private TelephonyManager telephonyManager;
    public SIMCardInfo(Context context) {
    telephonyManager = (TelephonyManager) context
    .getSystemService(Context.TELEPHONY_SERVICE);
    } public String getNativePhoneNumber() {
    String NativePhoneNumber = null;
    try{
    NativePhoneNumber = telephonyManager.getLine1Number();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    return NativePhoneNumber;
    }
    }
    主要是将Context context作为参数传进去,调用的时候如下调用即可:SIMCardInfo siminfo = new SIMCardInfo(MainActivity.this);
    String phoneNum = siminfo.getNativePhoneNumber();
    如果你上面那个类继承至Activity的话,会报错的.因为您一个类变量的构造函数需要激活系统服务,而这个类变量的实例化是直接在Activity类里面,而不是在OnCreate函数里面,需要放到OnCreate。
      

  5.   

    按着你说的可以了,我还想问一个问题,方便加下qq吗,QQ:574030493
      

  6.   

    如果我想做一个广告条的jar,应该什么思路呢,是不是就要用到资源文件了?
      

  7.   


    如果是banner类型广告比较简单,继承一下RelativeLayout什么的 public class BannerAdView extends RelativeLayout 
    然后通过addView把广告(一般是一个html页面放了一个banner,所以用webview)添加进去
    WebView web = new CustomWebView(context);
    LayoutParams params = getBannerLayoutParams();
    addView(web, params);
    web.loadUrl(adUrl);
      

  8.   

    由于res不太方便打包到jar中,则我们可以在代码里编写布局和控件什么的,如:
                   // 顶层布局
    mainLayout = new RelativeLayout(context);
    RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.FILL_PARENT,
    RelativeLayout.LayoutParams.FILL_PARENT);
    mainLayout.setBackgroundColor(0xffffffff);
    // 将顶层布局添加至Activity
    addContentView(mainLayout, mainParams); // 顶部显示进度条
    mProgressBar = new ProgressBar(context, null,
    android.R.attr.progressBarStyleHorizontal);
    LinearLayout.LayoutParams progressBarParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT, UnitUtil.dip2px(context,
    2));
    mProgressBar.setMax(100);
    mProgressBar.setMinimumHeight(UnitUtil.dip2px(context, 2));
    mainLayout.addView(mProgressBar, progressBarParams); // 底部显示按钮
    mBottomBarLayout = new LinearLayout(context);
    RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.FILL_PARENT, UnitUtil.dip2px(
    context, 45));
    bottomParams.alignWithParent = true;
    bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
    RelativeLayout.TRUE);
    mBottomBarLayout.setBackgroundColor(0xffececec);
    而图片什么的放到assets文件夹使用,如:               try {
    Bitmap bm = BitmapFactory.decodeStream(context.getAssets().open(
    "a.png"));
    mPreviousImg.setImageBitmap(bm);
    } catch (IOException e) {
    e.printStackTrace();
    }
      

  9.   

    非常感谢,学到了很多,我要做的广告条是需要自定义一个imageview,然后从网上down图片,然后图片轮播,点击相应的图片跳转到指定的网址即可。