- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在制作一个代码,根据其应用程序提供的屏幕尺寸更改其尺寸。它通过 View 的 onSizeChanged() 和 onDraw() 方法获取整个宽度和高度(或其相应 View 的 w 和 h)。
它在另一个代码中运行良好,但是现在每当我尝试输入命令“setLayoutParams()”时它都会显示一条错误消息您能帮我解决这个问题吗? setLayoutParams() 实际上在代码的任何地方都不起作用。下面是主要的类代码。
public class ManipulateDesign extends Activity { private LinearLayout ll; private ScrollView sv; private TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ll = (LinearLayout) findViewById(R.id.ll); sv = (ScrollView) findViewById(R.id.sv); tv = new TextView(this); tv.setText("You're in the first ScrollView\n" + "- Living Universe \n" + "Wish (upon a star) Hope to (be again)\n" + "The one you find, when all is done and through!\n" + "If all, made a wish to save, the star would it be in vain?\n" + "So much it holds, too dear to part, many ways to see the light of answers!\n" + "For all was once, just taken away, that one hope all disappeared so none can break nor brittle my heart\n" + "no, never again, until the end"); sv.addView(tv); ChangeSizeView csv = new ChangeSizeView(getApplicationContext()); ll.addView(csv); } class ChangeSizeView extends View{ int newWidth; int newHeight; public ChangeSizeView(Context context) { super(context); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { newWidth = w; newHeight = h; super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { // THIS COMMAND MAKES AN ERROR(not just in this method but anywhere in the entire code) BUT WORKED WELL IN ANOTHER CODE sv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, (newHeight - 100))); super.onDraw(canvas); } }}
And this is my main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ll"
>
<ScrollView android:id="@+id/sv"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ScrollView>
</LinearLayout>
这是显示错误信息的logcat标志
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): FATAL EXCEPTION: main
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.LinearLayout.measureVertical(LinearLayout.java:355)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.View.measure(View.java:8171)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.os.Looper.loop(Looper.java:123)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at java.lang.reflect.Method.invoke(Method.java:521)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 06:39:52.148: ERROR/AndroidRuntime(2952): at dalvik.system.NativeStart.main(Native Method)
最佳答案
你应该完全限定你需要的LayoutParams
,因为它包含在一个LinearLayout
中,你需要使用new LinearLayout.LayoutParams
关于android - setLayoutParams() 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5617492/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!