2011-02-11 61 views
1

最近,爲了將手機與無線傳感器網絡集成,我調整了一個類作爲活動以及從串口讀取並將一些讀數繪製成簡單的監聽器類基於畫布的視圖。 我最初嘗試使用更普通的消息處理程序和線程來構建一個線程畫布,該線程更新並使其無效,但存儲我需要繪製的讀數的類也正在關閉偵聽USB端口的線程。當時我無法得到它的工作,我決定我可能不需要不斷更新畫布,因爲我只需要用新的讀數更新它。視圖上的空指針

所以活動類如下。

public class Oscilloscope extends Activity implements MessageListener 

Graph graph; 
boolean guard=false; 
MoteIF mote; 
Data data; 
String comm; 
static String writeLog; 
public Vector <Integer> arrayvals = new Vector<Integer>(); 


public Oscilloscope(String comm, String writelog) 
{ 
    this.comm = comm; 
    this.writeLog = writelog; 
} 

public Oscilloscope() 
{ 

} 


public void onCreate(Bundle b) 
{ 
    super.onCreate(b); 
    graph = new Graph(this); 
    this.setContentView(graph); 
    if(guard==false) 
    { 
     create("dummy:1",null).run(); 
    } 

} 
synchronized public void messageReceived(int dest_addr, Message msg) { 
    if (msg instanceof OscilloscopeMsg) { 
     OscilloscopeMsg omsg = (OscilloscopeMsg)msg; 



     periodUpdate(omsg.get_version(), omsg.get_interval()); 
     data.update(omsg.get_id(), omsg.get_count(), omsg.get_readings()); 

     List temp1 = Arrays.asList(omsg.get_readings()); 
     Vector<Integer> temp= new Vector<Integer>(temp1); 

     this.arrayvals = temp; 
     Paint p = new Paint(); 
     p.setColor(Color.BLUE); 

     this.graph.update(); 
     this.graph.invalidate(); 

    } 
} 
public Oscilloscope create(String a, String b) 
{ 
    guard = true; 
    Oscilloscope os = new Oscilloscope(a,b); 
    os.guard = true; 
    return os; 
} 

不知道爲什麼它是我不能讓代碼括號在那裏正常工作,我道歉。 類中還有其他方法,但類本身的行爲應該是減去視圖部分,特別要注意消息接收方法,其中讀數以int數組讀取。 我的問題是,一旦onDraw方法被調用一次,然後調用messagereceived之後,圖形對象似乎是null,當我嘗試調用更新時,顯然會引發空指針。我可能沒有看到真正明顯的事情。 圖形代碼如下非常簡單:

public class Graph extends View 
    Paint paint= new Paint(); 
    public float x; 
    public float y; 
    Vector<CoOrdinate> coVec = new Vector<CoOrdinate>(); 

    public Graph(Context context) { 
     super(context); 
    } 

    public void onDraw(Canvas canvas) 
    { 


      Log.v("Line 28 Graph.java","Y: "+y+"X: "+x); 
      canvas.drawCircle(x, y, 2, paint); 
      Log.v(this.toString(),"onDraw in the graph"); 

    } 

    public void update() 
    { 
     Log.v("Line 31 of Graph.java",""); 
     x+=100; 
     y+=100; 
     paint.setColor(Color.GREEN); 
    } 
} 

任何幫助表示讚賞。謝謝。

02-11 19:03:00.933: DEBUG/AndroidRuntime(301): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 
02-11 19:03:00.933: DEBUG/AndroidRuntime(301): CheckJNI is ON 
02-11 19:03:02.012: DEBUG/AndroidRuntime(301): --- registering native functions --- 
02-11 19:03:03.942: INFO/ActivityManager(60): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=graphAndroid.graphs/.Oscilloscope } 
02-11 19:03:04.062: DEBUG/AndroidRuntime(301): Shutting down VM 
02-11 19:03:04.082: DEBUG/jdwp(301): adbd disconnected 
02-11 19:03:04.122: INFO/AndroidRuntime(301): NOTE: attach of thread 'Binder Thread #3' failed 
02-11 19:03:04.263: INFO/ActivityManager(60): Start proc graphAndroid.graphs for activity graphAndroid.graphs/.Oscilloscope: pid=308 uid=10040 gids={1015} 
02-11 19:03:05.263: INFO/ARMAssembler(60): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x361990:0x361a9c] in 6676000 ns 
02-11 19:03:05.493: INFO/System.out(308): Creating dummy source (dummy:1) 
02-11 19:03:05.493: INFO/System.out(308): No Motes:1 
02-11 19:03:05.503: VERBOSE/Line 129 Oscilloscope.java(308): Made it to here 
02-11 19:03:05.503: VERBOSE/Line 135 Oscilloscope.java(308): Made it here 
02-11 19:03:05.513: VERBOSE/Line 137 Oscilloscope.java(308): Made it here 
02-11 19:03:05.513: VERBOSE/Line 114 MoteIF.java(308): Third constructor 
02-11 19:03:05.513: VERBOSE/Line 121 MoteIF.java(308): Made it here 
02-11 19:03:05.513: VERBOSE/Line 123 MoteIF.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 127 MoteIF.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 130 MoteIF.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 69 PhoenixSource.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 71 PhoenixSource.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 74 PhoenixSource.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 133 MoteIF.java(308): Made it here 
02-11 19:03:05.523: VERBOSE/Line 138 MoteIF.java(308): Made it here 
02-11 19:03:05.533: VERBOSE/Line 140 MoteIF.java(308): Made it here 
02-11 19:03:05.543: VERBOSE/Line 142 MoteIF.java(308): Made it here 
02-11 19:03:05.543: VERBOSE/Line 138 Oscilloscope.java(308): Made it here 
02-11 19:03:05.543: VERBOSE/Line 140 Oscilloscope.java(308): Made it to here 
02-11 19:03:05.783: VERBOSE/Line 28 Graph.java(308): Y: 0.0X: 0.0 
02-11 19:03:05.793: VERBOSE/[email protected](308): onDraw in the graph 
02-11 19:03:05.823: INFO/ActivityManager(60): Displayed activity graphAndroid.graphs/.Oscilloscope: 1679 ms (total 1679 ms) 
02-11 19:03:07.543: INFO/System.out(308): SimulatedOscilloscopePacketSource moteId is 0 
02-11 19:03:07.543: VERBOSE/1(308): A test, line 424 OscilloscopeMsg class 
02-11 19:03:07.543: WARN/dalvikvm(308): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 
02-11 19:03:07.563: ERROR/AndroidRuntime(308): FATAL EXCEPTION: Thread-8 
02-11 19:03:07.563: ERROR/AndroidRuntime(308): java.lang.NullPointerException 
02-11 19:03:07.563: ERROR/AndroidRuntime(308):  at graphAndroid.graphs.Oscilloscope.messageReceived(Oscilloscope.java:108) 
02-11 19:03:07.563: ERROR/AndroidRuntime(308):  at net.tinyos.message.Receiver.packetReceived(Receiver.java:210) 
02-11 19:03:07.563: ERROR/AndroidRuntime(308):  at net.tinyos.packet.PhoenixSource.dispatch(PhoenixSource.java:165) 
02-11 19:03:07.563: ERROR/AndroidRuntime(308):  at net.tinyos.packet.PhoenixSource.packetDipatchLoop(PhoenixSource.java:157) 
02-11 19:03:07.563: ERROR/AndroidRuntime(308):  at net.tinyos.packet.PhoenixSource.run(PhoenixSource.java:174) 
02-11 19:03:07.613: WARN/ActivityManager(60): Force finishing activity graphAndroid.graphs/.Oscilloscope 
02-11 19:03:07.932: WARN/IInputConnectionWrapper(129): showStatusIcon on inactive InputConnection 
02-11 19:03:08.463: INFO/ARMAssembler(60): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x36b288:0x36b344] in 746000 ns 
+0

John,請添加堆棧跟蹤。 – DJC 2011-02-11 18:53:39

+0

抱歉,關於使用Logcat輸出編輯上面的那個。謝謝。 – 2011-02-11 19:07:22

回答

0

由於在onCreate被初始化,在新線程的領域將是null,爲onCreate只呼籲活動。我認爲這是墜機的直接原因。

我可能會重寫類爲兩個類(一個活動,一個線程),以避免混淆;但如果你想保持相同的總體結構,我建議你爲Oscilloscope添加第二個構造函數,它將Graph和其他共享成員作爲參數。調用此構造中create,以確保共享字段中傳遞(然後非常小心同步必要...)

編輯:在進一步檢查:圖形對象在傳遞到setContentViewonCreate。這意味着你一定不能嘗試在其他線程中使用它。看看使用MessageQueues和Handlers回發到UI線程,從那裏你可以安全地更新圖表。

順便說一句,你似乎沒有保存對任何地方的新線程的引用。我認爲這意味着新線程可以在任何時候被垃圾收集。

0

當你的活動被創建時,你創建了它的另一個實例(在create(string,string)中),但onCreate從來不會被新的實例調用。所以,現在,當在新的實例上調用messageReceived時,對象從未被實例化,並且您得到一個空指針異常。