我无法启动我的Android应用程序。我在fragment_main.xml中使用TableLayout。而且我认为在代码中使用它的问题。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.nikolay.calculator.MainActivity$PlaceholderFragment" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/firstNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="7"
android:hint="@string/hint1"
android:inputType="number|text" >
</EditText>
//....
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textResult"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textResult"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/result"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
这是我的MainActivity类。在initWidget()方法中,我为所有按钮设置onclicklistener并设置我的EditText和TextView字段。 onCreate方法是由eclipse创建的。我刚添加了两个最后的字符串。只提供一段代码。
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private CalcJavaLayer javaCalc;
private double a;
private double b;
private TableLayout table;
private EditText firstNum;
private EditText secondNum;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
javaCalc = new CalcJavaLayer();
initWidgets();
}
private void initWidgets(){
table = (TableLayout) findViewById(R.id.main_fragment);
for(int i = 0; i < table.getChildCount(); i++){
TableRow row = (TableRow) table.getChildAt(i);
for(int j = 0; j < row.getChildCount(); j++){
if(row.getChildAt(j) instanceof Button){
Button button = (Button) row.getChildAt(j);
button.setOnClickListener(this);
}else if(row.getChildAt(j) instanceof EditText){
firstNum = (EditText) row.getChildAt(0);
secondNum = (EditText) row.getChildAt(1);
break;
}else if(row.getChildAt(j) instanceof TextView){
result = (TextView) row.getChildAt(1);
break;
}
}
}
}
我收到下一个错误
05-20 14:45:35.649: E/AndroidRuntime(1701): FATAL EXCEPTION: main
05-20 14:45:35.649: E/AndroidRuntime(1701): Process: com.nikolay.calculator, PID: 1701
05-20 14:45:35.649: E/AndroidRuntime(1701): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nikolay.calculator/com.nikolay.calculator.MainActivity}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.TableLayout
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.os.Looper.loop(Looper.java:136)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 14:45:35.649: E/AndroidRuntime(1701): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 14:45:35.649: E/AndroidRuntime(1701): at java.lang.reflect.Method.invoke(Method.java:515)
05-20 14:45:35.649: E/AndroidRuntime(1701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-20 14:45:35.649: E/AndroidRuntime(1701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-20 14:45:35.649: E/AndroidRuntime(1701): at dalvik.system.NativeStart.main(Native Method)
05-20 14:45:35.649: E/AndroidRuntime(1701): Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.TableLayout
05-20 14:45:35.649: E/AndroidRuntime(1701): at com.nikolay.calculator.MainActivity.initWidgets(MainActivity.java:91)
05-20 14:45:35.649: E/AndroidRuntime(1701): at com.nikolay.calculator.MainActivity.onCreate(MainActivity.java:46)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.Activity.performCreate(Activity.java:5231)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-20 14:45:35.649: E/AndroidRuntime(1701): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-20 14:45:35.649: E/AndroidRuntime(1701): ... 11 more
谷歌的搜索没有产生结果。感谢任何帮助!