gpt4 book ai didi

java - Android,在方法之间传递 LinearLayout、RelativeLayout 和 TextView

转载 作者:行者123 更新时间:2023-11-30 03:06:14 24 4
gpt4 key购买 nike

我正在尝试创建一个更新特定 TextView 的线程。我已经在 onCreate() 方法中设置了 LinearLayout、RelativeLayout 和 TextView;我只想更新该信息。一切都编译得很好,只是当我在手机上执行它时,我遇到了强制关闭运行时错误。

我感觉问题在于 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);
}

我已将变量设为全局 Eureeka!我现在有一个运行时问题,它在声明处理程序时挂起:

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:32:56.204: E/AndroidRuntime(11184): ... 11 more

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

再次感谢您的帮助!

最佳答案

您的 NullPointerException 堆栈跟踪是由您对 View 创建所做的更改引起的。您不能像这样实例化 View :

public LinearLayout mainWindow = new LinearLayout(this);
public TextView mainText = new TextView(this);

作为成员变量,因为它们需要的Context(您的this 指针)在调用onCreate() 之前没有有效的资源。如果您想将对 View 的引用声明为类成员,那很好;但在 onCreate() 中实例化它们,a la:

public class MainActivity extends Activity {
public LinearLayout mainWindow;
public TextView mainText;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mainWindow = new LinearLayout(this);
mainText = new TextView(this);

mainWindow.addView(mainText);

setContentView(mainWindow);
}

}

您的旧代码可能遇到的崩溃是无法将 View 多次添加到父级,这样做会引发异常。在旧代码中,Handler 中的最后一个 mainWindow.addView() 和之后的 localWindow.addView() 尝试添加相同的内容TextView 到同一个父 View 两次。

关于java - Android,在方法之间传递 LinearLayout、RelativeLayout 和 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com