1

나는 과 TextView을 포함하는 LinearLayout입니다. 내 ConsoleWindow이 루프에서 실행 중이며 각 반복에서 TextView을 업데이트하고 싶습니다.문제 Android LinearLayout 사용

문제는 내가 EditText을 한 번만 (그렇지 않으면 액세스 할 수 없음) 초기화하고 LinearLayout을 한 번만 초기화 할 수 있습니다 (그렇지 않으면 EditText이 제거됨).

내가 만약 문에 LinearLayoutEditText을 넣을 수 없습니다

: layout.addView(tv);에서 IDE의 반환하기 때문에

if (firstRun) { 
    // initialize LinearLayout and EditText 
    firstRun = false; 
} 
// TEXTVIEW 
TextView tv = new TextView(getApplicationContext()) 
tv.setText(dataStringTot); 
layout.addView(tv); // "Qualifier must be an Expression." 

". 한정자가 표현되어야합니다"

내 코드 :

public boolean firstRun = true; 

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

getInetContent(); 
} 

getInetContent() { // would be a thread 
// getting data... 

Bundle b = new Bundle(); 
b.putString("StringNMEA", NMEA); 
Message m = mhandler.obtainMessage(); 
m.setData(b); 
mhandler.sendMessage(m); 
} 


Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     String dataString = ""; 
     Bundle bundle = msg.getData(); 

     if (bundle.containsKey("display")) { 
     ConsoleWindow(dataString); 
     } 
    } 
} 

private void ConsoleWindow(String dataString) { 
     if (firstRun) { 
      // initialize LinearLayout and EditText: 
      // LINEAR LAYOUT 
      LinearLayout layout = new LinearLayout(this); 
      setContentView(layout); 
      layout.setOrientation(LinearLayout.VERTICAL); 
      layout.setBackgroundColor(Color.parseColor("#000000")); // black 


      // EDITTEXT 
      EditText et = new EditText(getApplicationContext()); 
      et.setHint("Enter Command"); 
      layout.addView(et); 
      firstRun = false; 
     } 


      // TEXTVIEW 
      TextView tv = new TextView(getApplicationContext()); 
      tv.setText(dataStringTot); 
      layout.addView(tv); 
      } 
} 

가 어떻게이 문제를 해결할 수있는 다음과 같은 경우-문을

public class MainActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

getInetContent(); 
} 

getInetContent() { // would be a thread 
// getting data... 

Bundle b = new Bundle(); 
b.putString("StringNMEA", NMEA); 
Message m = mhandler.obtainMessage(); 
m.setData(b); 
mhandler.sendMessage(m); 
} 


Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     String dataString = ""; 
     Bundle bundle = msg.getData(); 

     if (bundle.containsKey("display")) { 
     ConsoleWindow(dataString); 
     } 
    } 
} 

private void ConsoleWindow(String dataString) { 

      // LINEAR LAYOUT 
      LinearLayout layout = new LinearLayout(this); 
      setContentView(layout); 
      layout.setOrientation(LinearLayout.VERTICAL); 
      layout.setBackgroundColor(Color.parseColor("#000000")); // black 


      // EDITTEXT 
      EditText et = new EditText(getApplicationContext()); 
      et.setHint("Enter Command"); 
      layout.addView(et); 


      // TEXTVIEW 
      TextView tv = new TextView(getApplicationContext()); 
      tv.setText(dataStringTot); 
      layout.addView(tv); 
      } 
} 

내 코드?

+1

전체 메서드 게시 – Blackbelt

+0

각 반복에서 setContentView (layout)을 호출 할 수 없습니다. – kelvincer

+0

그 이유는 if-Statement를 원했기 때문입니다 (첫 번째 실행에서 contentview 만 설정하기 때문에). 하지만 문제는 "한정어는 표현식이어야합니다"... – blackst0ne

답변

1

각 반복의 색상/텍스트를 업데이트하고 싶다고 상상해보십시오.

수정할 때마다 새보기를 만들 필요가 없습니다. 레이아웃을 설정하고 뷰를 얻은 다음 해당 뷰를 루프에 전달하십시오.

LinearLayout layout = new LinearLayout(this); 
EditText et = new EditText(this); 
TextView tv = new TextView(this); 
layout.addView(et); 
layout.addView(tv); 
setContentView(layout); 

et.setHint("Enter Command"); 
tv.setText(dataStringTot); 

startLoop(et, tv); 

public void startLoop(final EditText et, final TextView tv) { 
    ... 
} 

루프가 주 스레드에서 실행되지 않는 일종의 스레드 인 경우이 스레드를 주 스레드에서 실행하여 줄여야 할 수도 있습니다.

runOnUiThread(new Runnable() 
{ 
    public void run() 
    { 
     Log.v("mainActivity", "test"); 
    } 
}); 
+0

에 맞춰야한다고 제안했지만, IDE는 단지'LinearLayout layout = new LinearLayout (this); '을 내 ConsoleWindow() 외부에 놓을 수있게 해줍니다. 문제는 그 응용 프로그램이 정확히 그 줄에서 NullPointerException과 충돌한다는 것입니다. 어떤 생각? – blackst0ne