2014-10-03 72 views
-3

如何刪除錯誤,是t.start();如何在類中創建線程擴展片段

public class Timer extends Fragment { 

TextView tv1; 
Thread t1; 
Date d1; 
int hour,min,sec; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    final View android = inflater.inflate(R.layout.timer, container, false); 
    //((TextView)android.findViewById(R.id.textView)).setText("Timer"); 
    return android; 

    //tv1 = (TextView)findviewById(R.id.textView1); 

t1 = new Thread(){ 
    public void run(){ 
     try{ 
      while (!isInterrupted()) { 
       Thread.sleep(1000); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
        d1 = new Date(); 
        hour = d1.getHours(); 
        min = d1.getMinutes(); 
        sec = d1.getSeconds(); 
        String displaytime = hour+":"+min+":"+sec ; 
       // tv1.setText(displaytime); 
       ((TextView)android.findViewById(R.id.textView1)).setText(displaytime); 
         }});//runOnUiThread ends here 
       }//While ends here 
     }catch (InterruptedException e) { 
     }//Catch ends here 
    } 

/* Eclipse是示值誤差在下面t1.start(); */

t1.start(); 

    private void runOnUiThread(Runnable runnable) { 
     // TODO Auto-generated method stub 
    } 
};//Thread Ends here 
}//Oncreate 




} //Fragment 
+4

它顯示什麼錯誤? – Piyush 2014-10-03 05:30:01

回答

0

您正在螺紋在return聲明之後。

只需將return android;行移至線程結束後。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    final View android = inflater.inflate(R.layout.timer, container, false); 

t1 = new Thread(){ 
    public void run(){ 
     try{ 
      while (!isInterrupted()) { 
       Thread.sleep(1000); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
        d1 = new Date(); 
        hour = d1.getHours(); 
        min = d1.getMinutes(); 
        sec = d1.getSeconds(); 
        String displaytime = hour+":"+min+":"+sec ; 
       // tv1.setText(displaytime); 
       ((TextView)android.findViewById(R.id.textView1)).setText(displaytime); 
         }});//runOnUiThread ends here 
       }//While ends here 
     }catch (InterruptedException e) { 
     }//Catch ends here 
    } 
/* ECLIPSE IS SHOWING ERROR IN BELOW t1.start(); */ 

    t1.start(); 

    private void runOnUiThread(Runnable runnable) { 
     // TODO Auto-generated method stub 
    } 
};//Thread Ends here 


return android; 
}//Oncreate 

} //Fragment 
-1

請張貼錯誤日食顯示。 聲明「return android;」應該是oncreateview方法的最後一行。