本帖最后由 tongyu75 于 2010-04-14 11:00:44 编辑

解决方案 »

  1.   

    前面会定义new ResolveData(),但是没有返回对象。后面直接调用ResolveData.broken_line_List.get(i);这样的数据接口可能是由于你ResolveData没有对象,导致了问题。另外感觉你想要用单实例的模式来调用这个函数,可能ResolveData里面有一些设计问题,使得函数无法调用。检查顺序:
    1. ResolveData.broken_line_List.size(),看看返回值是多少。我估计是0.
    2. 如果第一步返回值不是零,请检查ResolveData.broken_line_List.get(i),进入函数内被去检查一下。如果能将代码再贴上是最好的。
      

  2.   

    broken_line_List这是个类变量吧?什么时候定义和初始化的?
      

  3.   


    以下两个类都放在了ResolveData类中了。
    public class ResolveData  {    //本类为省略的,我只拿出了主要内容。    
        public static List<Broken_line> broken_line_List = new ArrayList<Broken_line>();
        broken_line(byte*,int *); //调用broken_line方法,并且多次调用broken_line方法,所以用broken_line_List来添加对象broken。
        public static int broken_line(byte[] c,int j) {
    System.out.println("以下线的数据");
    Broken_line    broken = new Broken_line();
    broken.broken_point_number = c[j++];
    for(int s = 0; s<broken.broken_point_number; s++){
    broken.broken_x[s] = c[j++];
    System.out.println(broken.broken_x[s]);
    broken.broken_y[s] = c[j++];
    System.out.println(broken.broken_y[s]);
    }

    broken_line_List.add(broken);
    return j;
    }
    class Broken_line{     //这个类没有省略,是我程序中的原样
    public byte broken_point_number;
            public byte broken_x[] = new byte[5000];
            public byte broken_y[] = new byte[5000];
        
    public Broken_line(){

    }
    }
      

  4.   

    这是个List类的变量,在ResolveData中定义的,并且在ResolveData中初始化的。以下是我的部分代码请您帮忙看看。
    以下两个类都放在了ResolveData类中了。
    public class ResolveData { //本类为省略的,我只拿出了主要内容。    
      public static List<Broken_line> broken_line_List = new ArrayList<Broken_line>();
      broken_line(byte*,int *); //调用broken_line方法,并且多次调用broken_line方法,所以用  broken_line_List来添加对象broken。
      public static int broken_line(byte[] c,int j) {
         System.out.println("以下线的数据");
         Broken_line broken = new Broken_line();
         broken.broken_point_number = c[j++];
         for(int s = 0; s<broken.broken_point_number; s++){
            broken.broken_x[s] = c[j++];
            System.out.println(broken.broken_x[s]);
            broken.broken_y[s] = c[j++]; 
            System.out.println(broken.broken_y[s]);
         }      broken_line_List.add(broken);
         return j;
    }
    class Broken_line{ //这个类没有省略,是我程序中的原样
         public byte broken_point_number;
        public byte broken_x[] = new byte[5000];
        public byte broken_y[] = new byte[5000];
        
    public Broken_line(){}
    }
      

  5.   

    你说的很对,ResolveData.broken_line_List.size()的值的确为0,以下是部分代码:以下两个类都放在了ResolveData类中了。
    public class ResolveData { //本类为省略的,我只拿出了主要内容。    
      public static List<Broken_line> broken_line_List = new ArrayList<Broken_line>();
      broken_line(byte*,int *); //调用broken_line方法,并且多次调用broken_line方法,所以用  broken_line_List来添加对象broken。
      public static int broken_line(byte[] c,int j) {
      System.out.println("以下线的数据");
      Broken_line broken = new Broken_line();
      broken.broken_point_number = c[j++];
      for(int s = 0; s<broken.broken_point_number; s++){
      broken.broken_x[s] = c[j++];
      System.out.println(broken.broken_x[s]);
      broken.broken_y[s] = c[j++];  
      System.out.println(broken.broken_y[s]);
      }    broken_line_List.add(broken);
      return j;
    }
    class Broken_line{ //这个类没有省略,是我程序中的原样
      public byte broken_point_number;
      public byte broken_x[] = new byte[5000];
      public byte broken_y[] = new byte[5000];
        
    public Broken_line(){}
    }
      

  6.   

    其实我很像看ResolveData中的初始化是怎么样的。而且ResolveData这个类不是静态的,broken_line_List即使是静态的,你也不可以直接调用的,没有具体的对象,系统不知道该给你什么数据。解决方法一,将broken_line_List写成单实例的:private ResolveData mResolveData;
    public ResolveData getInstance(){
    if(mResolveData==null){
    mResolveData = new ResolveData();
    }
    return mResolveData;
    }
    通过ResolveData.getInstance().broken_line_List.get(i)来调用。
    解决方法二:
    public class MapView extends Activity {private ResolveData mResolveData;
      public void onCreate(Bundle savedInstanceState) {

      mResolveData = new ResolveData();

      }以后调用的时候,用mResolveData.broken_line_List.get(i)就OK了。
      

  7.   

    实在不好意思,我刚才给你的代码有错误,我忘把ResolveData中的初始化加上了,正确的ResolveData 类是下面的。还是请解答我最开始提出的问题,谢谢了,实在不好意思了!!!!!!
    以下两个类都放在了ResolveData类中了。
    public class ResolveData { //本类为省略的,我只拿出了主要内容。    
      public static List<Broken_line> broken_line_List = new ArrayList<Broken_line>();
      
      public ResolveData(){  
         .......
         这个类中有多次的循环来保存数据的
         broken_line(byte*,int *); //调用broken_line方法,并且多次调用broken_line方法
          .........
    }
      public static int broken_line(byte[] c,int j) {
      System.out.println("以下线的数据");
      Broken_line broken = new Broken_line();
      broken.broken_point_number = c[j++];
      for(int s = 0; s<broken.broken_point_number; s++){
      broken.broken_x[s] = c[j++];
      System.out.println(broken.broken_x[s]);
      broken.broken_y[s] = c[j++];   
      System.out.println(broken.broken_y[s]);
      }     broken_line_List.add(broken);
      return j;
    }
    class Broken_line{ //这个类没有省略,是我程序中的原样
      public byte broken_point_number;
      public byte broken_x[] = new byte[5000];
      public byte broken_y[] = new byte[5000];
        
    public Broken_line(){}
    }
      

  8.   

    实在不好意思,我刚才给你的代码有错误,我忘把ResolveData中的初始化加上了,正确的ResolveData 类是下面的。还是请解答我最开始提出的问题,谢谢了,实在不好意思了!!!!!!
    以下两个类都放在了ResolveData类中了。
    public class ResolveData { //本类为省略的,我只拿出了主要内容。    
      public static List<Broken_line> broken_line_List = new ArrayList<Broken_line>();
       
      public ResolveData(){   
      .......
      这个类中有多次的循环来保存数据的
      broken_line(byte*,int *); //调用broken_line方法,并且多次调用broken_line方法
      .........
    }
      public static int broken_line(byte[] c,int j) {
      System.out.println("以下线的数据");
      Broken_line broken = new Broken_line();
      broken.broken_point_number = c[j++];
      for(int s = 0; s<broken.broken_point_number; s++){
      broken.broken_x[s] = c[j++];
      System.out.println(broken.broken_x[s]);
      broken.broken_y[s] = c[j++];   
      System.out.println(broken.broken_y[s]);
      }     broken_line_List.add(broken);
      return j;
    }
    class Broken_line{ //这个类没有省略,是我程序中的原样
      public byte broken_point_number;
      public byte broken_x[] = new byte[5000];
      public byte broken_y[] = new byte[5000];
        
    public Broken_line(){}
    }
      

  9.   


    额, 看了你的构造函数,我想说的是,ResolveData这个类的对象不是静态的,所以即使是类中的变量时静态你也是无法通过静态的方法来调用的。除非你将ResolveData类的对象设置为静态的,或者在new的时候保存这个类的对象,你才可以正常的使用。解决方法,依然是我刚才说的第一种。请确认在调用其他类的函数一定要确认,类的对象是否是正确的。从你给的代码中可以看出,红色部分的代码ResolveData使用的临时变量,而不是一开始你new的那个,解决方法如我刚才说的第二种。
      

  10.   


    以下红色标记是按您的第二中方法做的,但还是不执行那个For循环即mResolveData.broken_line_List.size()=0。public class MapView extends Activity {    private static final String tag = null;
        private ResolveData mResolveData;
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mResolveData = new ResolveData();
            setContentView(new Map(this));   
        }
      
    class Map extends View{
            public Map(Context context) {   
                super(context);          
            }
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                int j = 0;
                Paint paint=new Paint();  
                Style style=Style.FILL_AND_STROKE;
                paint.setStyle(style);
                paint.setColor(Color.BLUE);   
                float[] a = {100l, 50l,200l, 50l,10l, 20l, 10l,50l};
                for(int i = 0; i<mResolveData.broken_line_List.size();i++){      
                 Log.e(tag,"BBBBBBBBBBBBBBBB");  
                   Broken_line br= mResolveData.broken_line_List.get(i);
                   canvas.drawLine(100,200,10,30, paint);
                for(int kk = 0 ; kk<br.broken_point_number;kk++){
               canvas.drawLine(br.broken_x[kk]+128,br.broken_y[kk],br.broken_x[kk+1]     +725,br.broken_y[kk+1],paint);            
               }  
               }
                
               
            }   
        }
    }
      

  11.   

    我刚才按照你的结构试了一下,发现onDraw这个函数根本没有跑到,所以你的数据就没有出来。
      

  12.   


    对不起我错了,都跑到了。我给你看下我的测试代码,你的代码我没有看全。
     package com.android.test;import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.LinearLayout;public class test extends Activity {    private Button mButton;
        
        private statictest mTest;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);
            
            mTest = new statictest(this);
            setContentView(new TestView(this));
            
    /*        mButton = (Button)findViewById(R.id.button);
            mButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                // TODO Auto-generated method stub
                    Log.i("test", "mTest.mBitmapList.size() " + mTest.mBitmapList.size());
                }
            });*/
        }
        
        class TestView extends LinearLayout{        public TestView(Context context) {
                super(context);
                Log.i("test", "TestView mTest.mBitmapList.size() " + mTest.mBitmapList.size());
                // TODO Auto-generated constructor stub
            }        
            
            @Override
            protected void dispatchDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.dispatchDraw(canvas);
                Log.i("test", "dispatchDraw mTest.mBitmapList.size() " + mTest.mBitmapList.size());
            }        @Override
            protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                super.onDraw(canvas);
                Log.i("test", "onDraw mTest.mBitmapList.size() " + mTest.mBitmapList.size());
            }
        }
    }
    package com.android.test;import java.util.ArrayList;
    import java.util.List;import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.drawable.BitmapDrawable;
    public class statictest{
        
        public static List<Bitmap> mBitmapList = new ArrayList<Bitmap>();
        
        private Context mContext;
        
        
        
        public statictest(Context context){
            mContext = context;
            Bitmap b = ((BitmapDrawable)mContext.getResources().getDrawable(R.drawable.icon)).getBitmap();
            
            for(int i = 0; i < 10; i++){
                storeList(b);
            }
        }
        
        public static void storeList(Bitmap b){
            mBitmapList.add(b);
        }
        
    }
      

  13.   

    非常感谢帮我跑程序,我也做了一个测试类如下:能够完全运行。而且我用的是类名.变量也一样好使,这是为什么呢?
    public class A  {
    public static void main(String args[]){
       new ResolveData(); //为了初始化ResolveData类中的数据
           for(int m = 0; m<ResolveData.broken_line_List.size();m++){
               Broken_line line = ResolveData.broken_line_List.get(m);
              for(int kk = 0 ; kk<line.broken_point_number;kk++){
              System.out.println(line.broken_x[kk]);
                       System.out.println(line.broken_y[kk]);
              }
           }

    }
    }
            
      

  14.   

    如果方便请把你的source发给我,我帮你看下吧。这样用网页看很痛苦啊~~~
      

  15.   


    非常感谢帮我跑程序,我也做了一个测试类如下:能够完全运行。而且我用的是类名.变量也一样好使,这是为什么呢?
    public class A {
       public static void main(String args[]){
       new ResolveData(); //为了初始化ResolveData类中的数据
       for(int m = 0; m<ResolveData.broken_line_List.size();m++){
          Broken_line line = ResolveData.broken_line_List.get(m);
          for(int kk = 0 ; kk<line.broken_point_number;kk++){
             System.out.println(line.broken_x[kk]);
             System.out.println(line.broken_y[kk]);
       }
      } }
    }
      
      

  16.   


    我找到错误了,因为我在ResolveData类的初始化中使用了File file = new File("D:\\***");这句话没有放到Android工程文件下的assets文件夹下,所以就不能显示在模拟器中了。