这是导致此问题的代码:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
double height = displaymetrics.heightPixels;
double width = displaymetrics.widthPixels;
int Orientation = getResources().getConfiguration().orientation;
if(Orientation == Configuration.ORIENTATION_PORTRAIT)
{
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setLayoutParams(new LayoutParams(5,5));
}
当我把它放在我的完整代码中时:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DecimalFormat currencyFormatter = (DecimalFormat) NumberFormat.getInstance();
char decimalSeparator = currencyFormatter.getDecimalFormatSymbols().getDecimalSeparator();
mDecimalSeparator = Character.toString(decimalSeparator);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
double height = displaymetrics.heightPixels;
double width = displaymetrics.widthPixels;
int Orientation = getResources().getConfiguration().orientation;
if(Orientation == Configuration.ORIENTATION_PORTRAIT)
{
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setLayoutParams(new LayoutParams(5,5));
}
setContentView(R.layout.main);
为什么这会发生我得到的消息是app停止工作。
这是我的logcat:
03-14 19:14:42.067: E/AndroidRuntime(12380): FATAL EXCEPTION: main
03-14 19:14:42.067: E/AndroidRuntime(12380): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableRow.mapIndexAndColumns(TableRow.java:164)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableRow.getVirtualChildCount(TableRow.java:149)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableRow.getColumnsWidths(TableRow.java:289)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableLayout.findLargestCells(TableLayout.java:508)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableLayout.measureVertical(TableLayout.java:473)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.TableLayout.onMeasure(TableLayout.java:439)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.View.measure(View.java:15635)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1220)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.ScrollView.onMeasure(ScrollView.java:321)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.View.measure(View.java:15635)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.View.measure(View.java:15635)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.View.measure(View.java:15635)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-14 19:14:42.067: E/AndroidRuntime(12380): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.View.measure(View.java:15635)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.Choreographer.doFrame(Choreographer.java:548)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.os.Handler.handleCallback(Handler.java:800)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.os.Handler.dispatchMessage(Handler.java:100)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.os.Looper.loop(Looper.java:194)
03-14 19:14:42.067: E/AndroidRuntime(12380): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-14 19:14:42.067: E/AndroidRuntime(12380): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 19:14:42.067: E/AndroidRuntime(12380): at java.lang.reflect.Method.invoke(Method.java:525)
03-14 19:14:42.067: E/AndroidRuntime(12380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-14 19:14:42.067: E/AndroidRuntime(12380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-14 19:14:42.067: E/AndroidRuntime(12380): at dalvik.system.NativeStart.main(Native Method)
这是我的main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
<TableRow
android:id="@+id/tableRow1"
android:weightSum="1" >
<TextView
android:id="@+id/ResultText"
android:gravity="right"
android:textSize="20sp"
android:background="#AAAAAA"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:weightSum="1" >
<TextView
android:id="@+id/InputText"
android:background="#BBBBBB"
android:gravity="right"
android:textColor="#FFFFFF"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:textSize="15sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow0"
android:layout_width="wrap_content"
android:weightSum="1" >
<TextView
android:id="@+id/memoryStatText"
android:background="#BBBBBB"
android:gravity="right"
android:textColor="#FFFFFF"
android:layout_weight="1"
android:layout_marginTop="4dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:weightSum="1" >
<Button
android:id="@+id/buttonMAdd"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="M+"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonMRemove"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="M-"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonMC"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="MC"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonMR"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="MR"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonClearAll"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="C"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonDel"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="del"
android:gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:weightSum="1" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="1"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="2"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="3"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="4"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonAdd"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="+"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonSubtract"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="10dp"
android:layout_weight="0.166"
android:text="-"
android:onClick="ProcessInput"
android:gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:weightSum="1" >
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="5"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="6"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="7"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="8"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonDivide"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="/"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonMultiply"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="x"
android:onClick="ProcessInput"
android:gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:weightSum="1"
android:gravity="fill" >
<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="9"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/button0"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="0"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonPeriod"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="."
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonSign"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="±"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonPercent"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:text="%"
android:onClick="ProcessInput" />
<Button
android:id="@+id/buttonEqual"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:text="="
android:onClick="ProcessInput" />
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:weightSum="1" >
<Button
android:id="@+id/buttonSin"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:text="sin"
android:onClick="ProcessInput" />
<Button
android:id="@+id/buttonCos"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:text="cos"
android:onClick="ProcessInput" />
<Button
android:id="@+id/buttonTan"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:text="tan"
android:onClick="ProcessInput" />
<Button
android:id="@+id/buttonSinInverse"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:textSize="8sp"
android:gravity="fill"
android:text="@string/sin_inv"
android:onClick="ProcessInput"/>
<Button
android:id="@+id/buttonCosInverse"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:textSize="8sp"
android:text="@string/cos_inv"
android:onClick="ProcessInput"/>
<Button
android:id="@+id/buttonTanInverse"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:gravity="fill"
android:textSize="8sp"
android:text="@string/tan_inv"
android:onClick="ProcessInput"/>
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:weightSum="1" >
<Button
android:id="@+id/buttonPower"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:textSize="12sp"
android:text="pow"
android:gravity="fill"
android:onClick="ProcessInput" />
<Button
android:id="@+id/buttonLog10"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="log"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonLoge"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="ln"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonSquare"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="sqr"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonSquareRoot"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="sqrt"
android:onClick="ProcessInput"
android:gravity="fill" />
<Button
android:id="@+id/buttonFactorial"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="0.166"
android:text="x!"
android:onClick="ProcessInput"
android:gravity="fill" />
</TableRow>
</TableLayout>
</ScrollView>
答案 0 :(得分:1)
像这样更新
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DecimalFormat currencyFormatter = (DecimalFormat) NumberFormat.getInstance();
char decimalSeparator = currencyFormatter.getDecimalFormatSymbols().getDecimalSeparator();
mDecimalSeparator = Character.toString(decimalSeparator);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
double height = displaymetrics.heightPixels;
double width = displaymetrics.widthPixels;
int Orientation = getResources().getConfiguration().orientation;
if(Orientation == Configuration.ORIENTATION_PORTRAIT)
{
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setLayoutParams(new LayoutParams(5,5));
}
您已将setContentView(R.layout.main);
放在最后。
更新的答案
这是问题
btn1.setLayoutParams(new LayoutParams(5,5));
您的按钮必须位于表格布局中,并且您使用了错误的布局参数。检查您正在使用的布局参数。
答案 1 :(得分:0)
改变你的
import android.view.ViewGroup.LayoutParams;
是
import android.widget.TableRow.LayoutParams;