我制作了一个完美运行的计算器程序,用于练习 android。我不喜欢我开始的相对布局,并决定尝试网格布局。现在我MainActivity.java
没有看到activity_main.xml
文件。我检查了R.java
文件并查看了 的列表activity_main.xml
,其中列出了所有按钮。activity_main.xml
解析就好了。我试过重建程序无济于事。任何想法都会有所帮助!这是activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alignmentMode="alignBounds"
android:columnCount="8"
android:columnOrderPreserved="false"
android:useDefaultMargins="true" >
<EditText
android:id="@+id/total"
android:layout_column="0"
android:layout_columnSpan="6"
android:layout_gravity="left"
android:layout_row="0"
android:clickable="false"
android:ems="10"
android:inputType="none|number"
android:textAlignment="textEnd"
android:width="500dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/Button8"
android:layout_column="1"
android:layout_row="1"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="8" />
<Button
android:id="@+id/Button9"
android:layout_column="2"
android:layout_row="1"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="9" />
<Button
android:id="@+id/Button4"
android:layout_column="0"
android:layout_row="2"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="4" />
<Button
android:id="@+id/Button5"
android:layout_column="1"
android:layout_row="2"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="5" />
<Button
android:id="@+id/Button6"
android:layout_column="2"
android:layout_row="2"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="6" />
<Button
android:id="@+id/ButtonMultiply"
android:layout_column="3"
android:layout_row="2"
android:background="@drawable/custom_button"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="Multiply"
android:text="*"
android:textColor="@color/light_grey" />
<Button
android:id="@+id/Button1"
android:layout_column="0"
android:layout_row="3"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="1" />
<Button
android:id="@+id/Button2"
android:layout_column="1"
android:layout_row="3"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="2" />
<Button
android:id="@+id/Button3"
android:layout_column="2"
android:layout_row="3"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="3" />
<Button
android:id="@+id/ButtonMinus"
android:layout_column="3"
android:layout_row="3"
android:background="@drawable/custom_button"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="Subtract"
android:text="-"
android:textColor="@color/light_grey" />
<Button
android:id="@+id/ButtonDivide"
android:layout_column="3"
android:layout_gravity="right"
android:layout_row="1"
android:layout_rowSpan="2"
android:background="@drawable/custom_button"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="Divide"
android:text="/"
android:textColor="@color/light_grey" />
<Button
android:id="@+id/button0"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_row="4"
android:background="@drawable/button_light_grey"
android:minHeight="20dp"
android:minWidth="82dp"
android:onClick="numberButtonEventHandler"
android:text="0" />
<Button
android:id="@+id/buttonPoint"
android:layout_column="2"
android:layout_row="4"
android:background="@drawable/button_light_grey"
android:minHeight="20dip"
android:minWidth="40dp"
android:onClick="numberButtonEventHandler"
android:text="."
android:width="20dip" />
<Button
android:id="@+id/ButtonPlus"
android:layout_column="3"
android:layout_row="4"
android:background="@drawable/custom_button"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="Add"
android:text="+"
android:textColor="@color/light_grey" />
<Button
android:id="@+id/ButtonEqueals"
android:layout_column="4"
android:layout_row="3"
android:layout_rowSpan="2"
android:background="@drawable/custom_button"
android:height="40dp"
android:minHeight="82dp"
android:minWidth="20dp"
android:onClick="Equeals"
android:text="="
android:textColor="@color/light_grey"
android:width="40dp" />
<Button
android:id="@+id/Button7"
android:layout_column="0"
android:layout_row="1"
android:background="@drawable/button_light_grey"
android:minHeight="40dip"
android:minWidth="40dip"
android:onClick="numberButtonEventHandler"
android:text="7" />
</android.support.v4.widget.GridLayout>
这是MainActivity.java
文件
package com.example.calculator;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
private String textTotal = "";
Total total = new Total();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <---here I get my first error
eclipse help suggests changing
//setButtonListeners(); to activity_list_item. This
} doesn't help
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void numberButtonEventHandler(View v) {
textTotal += ((Button) v).getText();
TextView total = (TextView)findViewById(R.id.total);
total.setText(textTotal);
}
public void Add(View v){
try {
total.doMath(Double.parseDouble(textTotal));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setOperater("+");
TextView TextViewtotal = (TextView)findViewById(R.id.total);
TextViewtotal.setText(Double.toString(total.getTotalValue()));
textTotal = "";
}
public void Subtract(View v){
try {
total.doMath(Double.parseDouble(textTotal));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setOperater("-");
TextView TextViewtotal = (TextView)findViewById(R.id.total);
TextViewtotal.setText(Double.toString(total.getTotalValue()));
textTotal = "";
}
public void Multiply(View v){
try {
total.doMath(Double.parseDouble(textTotal));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setOperater("*");
TextView TextViewtotal = (TextView)findViewById(R.id.total);
TextViewtotal.setText(Double.toString(total.getTotalValue()));
textTotal = "";
}
public void Divide(View v){
try {
total.doMath(Double.parseDouble(textTotal));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setOperater("/");
TextView TextViewtotal = (TextView)findViewById(R.id.total);
TextViewtotal.setText(Double.toString(total.getTotalValue()));
textTotal = "";
}
public void Equeals(View v){
try {
total.doMath(Double.parseDouble(textTotal));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total.setOperater("");
TextView TextViewtotal = (TextView)findViewById(R.id.total);
TextViewtotal.setText(Double.toString(total.getTotalValue()));
textTotal = "";
}
}
现在控制台输出:
[2013-04-03 22:19:26 - Calculator] Could not find Calculator.apk!
[2013-04-03 22:19:53 - Calculator] ------------------------------
[2013-04-03 22:19:53 - Calculator] Android Launch!
[2013-04-03 22:19:53 - Calculator] adb is running normally.
[2013-04-03 22:19:53 - Calculator] Could not find Calculator.apk!
[2013-04-03 22:20:58 - Calculator] ------------------------------
[2013-04-03 22:20:58 - Calculator] Android Launch!
[2013-04-03 22:20:58 - Calculator] adb is running normally.
[2013-04-03 22:20:58 - Calculator] Could not find Calculator.apk!
[2013-04-03 22:24:29 - Calculator] ------------------------------
[2013-04-03 22:24:29 - Calculator] Android Launch!
[2013-04-03 22:24:29 - Calculator] adb is running normally.
[2013-04-03 22:24:29 - Calculator] Could not find Calculator.apk!
下一个 logcat 输出:
04-03 22:46:16.121: E/AndroidRuntime(17973): FATAL EXCEPTION: main
04-03 22:46:16.121: E/AndroidRuntime(17973): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.GridLayout
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.os.Looper.loop(Looper.java:123)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-03 22:46:16.121: E/AndroidRuntime(17973): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 22:46:16.121: E/AndroidRuntime(17973): at java.lang.reflect.Method.invoke(Method.java:521)
04-03 22:46:16.121: E/AndroidRuntime(17973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-03 22:46:16.121: E/AndroidRuntime(17973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-03 22:46:16.121: E/AndroidRuntime(17973): at dalvik.system.NativeStart.main(Native Method)
04-03 22:46:16.121: E/AndroidRuntime(17973): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.GridLayout
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-03 22:46:16.121: E/AndroidRuntime(17973): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:200)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.Activity.setContentView(Activity.java:1647)
04-03 22:46:16.121: E/AndroidRuntime(17973): at com.example.calculator.MainActivity.onCreate(MainActivity.java:17)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-03 22:46:16.121: E/AndroidRuntime(17973): ... 11 more
04-03 22:46:16.121: E/AndroidRuntime(17973): Caused by: java.lang.ClassNotFoundException: android.support.v4.widget.GridLayout in loader dalvik.system.PathClassLoader[/data/app/com.example.calculator-1.apk]
04-03 22:46:16.121: E/AndroidRuntime(17973): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-03 22:46:16.121: E/AndroidRuntime(17973): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-03 22:46:16.121: E/AndroidRuntime(17973): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
04-03 22:46:16.121: E/AndroidRuntime(17973): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
04-03 22:46:16.121: E/AndroidRuntime(17973): ... 19 more