특정 TextView를 업데이트하는 스레드를 만들려고합니다. onCreate() 메서드에서 LinearLayout, RelativeLayout 및 TextView를 이미 설정했습니다. 나는 단지 그 정보를 업데이트하기를 원할 뿐이다. 모든 것이 잘 컴파일됩니다. 휴대 전화에서 실행하면 강제 실행 시간 오류가 발생합니다.메소드간에 LinearLayout, RelativeLayout 및 TextView를 전달하는 Android
문제가 onRefresh (...) {와 관련되어 있고 그 정보를 어떻게 전달했는지 알고 있습니다. 누구든지 정보를 올바르게 전달하는 방법을 명확히 할 수 있습니까?
감사합니다.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Define String(s)
String infoText = "Push button to test";
String buttonLabelA = "Show Hello!";
//Setup screen orientation
LinearLayout mainWindow = new LinearLayout(this);
mainWindow.setOrientation(LinearLayout.VERTICAL);
//Set title
setTitle("JOHello");
//Set the static text that will appear in the window
TextView label = new TextView(this);
label.setText(infoText);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(400, 50);
params1.leftMargin = 15;
params1.topMargin = 5;
mainWindow.addView(label, params1);
//Set button
Button greetingButton = new Button(this);//instantiate greeting button
greetingButton.setText(buttonLabelA);//set button text
mVB localVB = new mVB();
greetingButton.setOnClickListener(localVB);//link button click to event
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(200, 75);
params2.leftMargin = 100;
params2.topMargin = 100;
mainWindow.addView(greetingButton, params2);//add button to current view*/
//Changeable text
TextView chgTxt = new TextView(this);
chgTxt.setText(localVB.greetingText);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(400, 50);
params3.leftMargin = 15;
params3.topMargin = 600;
mainWindow.addView(chgTxt, params3);
setContentView(mainWindow);
doRefresh(mainWindow, params3, chgTxt);
}
private void doRefresh(final LinearLayout localWindow, final RelativeLayout.LayoutParams localParams, final TextView localText){
handler.postDelayed(new Runnable() {
@Override
public void run() {
localText.setText("It truly works!");
localWindow.addView(localText, localParams);
}
}, 1000);
}
글로벌 변수를 만들었습니다!
02-13 23 : 32 : 56.204 : 여기
로그 고양이 :이 핸들러가 선언 될 때 중단 어디 지금 런타임 문제가 E/AndroidRuntime (11184) : ... 당신의 도움들 주셔서 다시 11 개 개의public class MainActivity extends Activity { private final Handler handler = new Handler();//Hanging!? public LinearLayout mainWindow = new LinearLayout(this); public TextView mainText = new TextView(this); public RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(400, 50); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } public void doRefresh(){ handler.postDelayed(new Runnable() { @Override public void run() { mainText.setText("It fucking works"); mainWindow.addView(mainText, mainParams); } }, 1000); }
02-13 23:33:43.500: I/dalvikvm(11215): Debugger is active
02-13 23:33:43.580: I/System.out(11215): Debugger has connected
02-13 23:33:43.580: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:43.781: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:43.981: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:44.181: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:44.381: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:44.581: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:44.782: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:44.982: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:45.182: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:45.372: I/System.out(11215): waiting for debugger to settle...
02-13 23:33:45.582: I/System.out(11215): debugger has settled (1410)
02-13 23:34:14.651: D/AndroidRuntime(11215): Shutting down VM
02-13 23:34:14.661: W/dalvikvm(11215): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
02-13 23:34:14.711: E/AndroidRuntime(11215): FATAL EXCEPTION: main
02-13 23:34:14.711: E/AndroidRuntime(11215): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.johello/com.johello.MainActivity}: java.lang.NullPointerException
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1738)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.os.Looper.loop(Looper.java:143)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread.main(ActivityThread.java:4196)
02-13 23:34:14.711: E/AndroidRuntime(11215): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 23:34:14.711: E/AndroidRuntime(11215): at java.lang.reflect.Method.invoke(Method.java:507)
02-13 23:34:14.711: E/AndroidRuntime(11215): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-13 23:34:14.711: E/AndroidRuntime(11215): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-13 23:34:14.711: E/AndroidRuntime(11215): at dalvik.system.NativeStart.main(Native Method)
02-13 23:34:14.711: E/AndroidRuntime(11215): Caused by: java.lang.NullPointerException
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.view.View.<init>(View.java:1915)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.view.ViewGroup.<init>(ViewGroup.java:299)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.widget.LinearLayout.<init>(LinearLayout.java:116)
02-13 23:34:14.711: E/AndroidRuntime(11215): at com.johello.MainActivity.<init>(MainActivity.java:16)
02-13 23:34:14.711: E/AndroidRuntime(11215): at java.lang.Class.newInstanceImpl(Native Method)
02-13 23:34:14.711: E/AndroidRuntime(11215): at java.lang.Class.newInstance(Class.java:1409)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-13 23:34:14.711: E/AndroidRuntime(11215): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
02-13 23:34:14.711: E/AndroidRuntime(11215): ... 11 more
감사합니다!
logcat을 게시하십시오 – codeMagic
로컬 변수 대신 전역 변수를 선언하면 스레드가 사용할 수 있습니다 –
'mainWindow'에'TextView' chgTxt를 한 번 추가했습니다. 왜 당신은'doRefresh()'메쏘드 안에 다시 추가하고 싶습니까? – TactMayers