从第二个 Java 文件调用函数时崩溃
我对 Android 游戏比较陌生,每当我从第二个 java 类调用函数时,我的应用程序就会崩溃。
这是我的主要 JAVA 文件:
package mashuda.tools.calculator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CalculatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Drawinterface layout = new Drawinterface();
setContentView(layout.drawit());
}
}
它正在调用此代码:
package mashuda.tools.calculator;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Drawinterface extends Activity {
/** Called when the activity is first created.
* @return */
TableLayout drawit(){
TableLayout maintable = new TableLayout(this);
maintable.setStretchAllColumns(true);
maintable.setShrinkAllColumns(true);
TableRow banner = new TableRow(this);
banner.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowOutput = new TableRow(this);
TableRow rowSeven = new TableRow(this);
TableRow rowFour = new TableRow(this);
TableRow rowOne = new TableRow(this);
TableRow rowZero = new TableRow(this);
//answer box
TextView answer = new TextView(this);
answer.setText("ANSWER");
answer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
answer.setBackgroundColor(-1);
answer.setGravity(Gravity.RIGHT);
answer.setTypeface(Typeface.SERIF);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 5;
//loads answer bar
rowOutput.addView(answer, params);
//first column
//creates 7 key
Button sevenkey = new Button(this);
sevenkey.setText("7");
sevenkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 8 key
Button eightkey = new Button(this);
eightkey.setText("8");
eightkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 9 key
Button ninekey = new Button(this);
ninekey.setText("9");
ninekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates division key
Button dividekey = new Button(this);
dividekey.setText("/");
dividekey.setTypeface(Typeface.DEFAULT_BOLD);
// loads top row
rowSeven.addView(sevenkey);
rowSeven.addView(eightkey);
rowSeven.addView(ninekey);
rowSeven.addView(dividekey);
//creates 4 key
Button fourkey = new Button(this);
fourkey.setText("4");
fourkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 5 key
Button fivekey = new Button(this);
fivekey.setText("5");
fivekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 6 key
Button sixkey = new Button(this);
sixkey.setText("6");
sixkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates multiplication key
Button multiplykey = new Button(this);
multiplykey.setText("X");
multiplykey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into second row
rowFour.addView(fourkey);
rowFour.addView(fivekey);
rowFour.addView(sixkey);
rowFour.addView(multiplykey);
//creates 1 key
Button onekey = new Button(this);
onekey.setText("1");
onekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 2 key
Button twokey = new Button(this);
twokey.setText("2");
twokey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 3 key
Button threekey = new Button(this);
threekey.setText("3");
threekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates add key
Button addkey = new Button(this);
addkey.setText("+");
addkey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into third row
rowOne.addView(onekey);
rowOne.addView(twokey);
rowOne.addView(threekey);
rowOne.addView(addkey);
//creates 0 key
Button zerokey = new Button(this);
zerokey.setText("0");
zerokey.setTypeface(Typeface.DEFAULT_BOLD);
// creates . key
Button decimalkey = new Button(this);
decimalkey.setText(".");
decimalkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates - key
Button negativekey = new Button(this);
negativekey.setText("+/-");
negativekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates equals key
Button equalskey = new Button(this);
equalskey.setText("=");
equalskey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into fourth row
rowZero.addView(zerokey);
rowZero.addView(decimalkey);
rowZero.addView(negativekey);
rowZero.addView(equalskey);
// loads rows into table
maintable.addView(rowOutput);
maintable.addView(rowSeven);
maintable.addView(rowFour);
maintable.addView(rowOne);
maintable.addView(rowZero);
//sets view as maintable
return (maintable);
}
}
我确实将第二个文件添加到了 android 清单中。 我的编译器(eclipse)没有检测到任何错误。 任何帮助将不胜感激
I am relatively new to the android game and I am having an issue with my app crashing whenever I call a function from a second java class.
this is my main JAVA file:
package mashuda.tools.calculator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class CalculatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Drawinterface layout = new Drawinterface();
setContentView(layout.drawit());
}
}
and it is calling this code:
package mashuda.tools.calculator;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Drawinterface extends Activity {
/** Called when the activity is first created.
* @return */
TableLayout drawit(){
TableLayout maintable = new TableLayout(this);
maintable.setStretchAllColumns(true);
maintable.setShrinkAllColumns(true);
TableRow banner = new TableRow(this);
banner.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowOutput = new TableRow(this);
TableRow rowSeven = new TableRow(this);
TableRow rowFour = new TableRow(this);
TableRow rowOne = new TableRow(this);
TableRow rowZero = new TableRow(this);
//answer box
TextView answer = new TextView(this);
answer.setText("ANSWER");
answer.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
answer.setBackgroundColor(-1);
answer.setGravity(Gravity.RIGHT);
answer.setTypeface(Typeface.SERIF);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 5;
//loads answer bar
rowOutput.addView(answer, params);
//first column
//creates 7 key
Button sevenkey = new Button(this);
sevenkey.setText("7");
sevenkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 8 key
Button eightkey = new Button(this);
eightkey.setText("8");
eightkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 9 key
Button ninekey = new Button(this);
ninekey.setText("9");
ninekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates division key
Button dividekey = new Button(this);
dividekey.setText("/");
dividekey.setTypeface(Typeface.DEFAULT_BOLD);
// loads top row
rowSeven.addView(sevenkey);
rowSeven.addView(eightkey);
rowSeven.addView(ninekey);
rowSeven.addView(dividekey);
//creates 4 key
Button fourkey = new Button(this);
fourkey.setText("4");
fourkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 5 key
Button fivekey = new Button(this);
fivekey.setText("5");
fivekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 6 key
Button sixkey = new Button(this);
sixkey.setText("6");
sixkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates multiplication key
Button multiplykey = new Button(this);
multiplykey.setText("X");
multiplykey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into second row
rowFour.addView(fourkey);
rowFour.addView(fivekey);
rowFour.addView(sixkey);
rowFour.addView(multiplykey);
//creates 1 key
Button onekey = new Button(this);
onekey.setText("1");
onekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 2 key
Button twokey = new Button(this);
twokey.setText("2");
twokey.setTypeface(Typeface.DEFAULT_BOLD);
// creates 3 key
Button threekey = new Button(this);
threekey.setText("3");
threekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates add key
Button addkey = new Button(this);
addkey.setText("+");
addkey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into third row
rowOne.addView(onekey);
rowOne.addView(twokey);
rowOne.addView(threekey);
rowOne.addView(addkey);
//creates 0 key
Button zerokey = new Button(this);
zerokey.setText("0");
zerokey.setTypeface(Typeface.DEFAULT_BOLD);
// creates . key
Button decimalkey = new Button(this);
decimalkey.setText(".");
decimalkey.setTypeface(Typeface.DEFAULT_BOLD);
// creates - key
Button negativekey = new Button(this);
negativekey.setText("+/-");
negativekey.setTypeface(Typeface.DEFAULT_BOLD);
// creates equals key
Button equalskey = new Button(this);
equalskey.setText("=");
equalskey.setTypeface(Typeface.DEFAULT_BOLD);
// loads keys into fourth row
rowZero.addView(zerokey);
rowZero.addView(decimalkey);
rowZero.addView(negativekey);
rowZero.addView(equalskey);
// loads rows into table
maintable.addView(rowOutput);
maintable.addView(rowSeven);
maintable.addView(rowFour);
maintable.addView(rowOne);
maintable.addView(rowZero);
//sets view as maintable
return (maintable);
}
}
I did add the second file to the android manifest.
and my compiler(eclipse) does not detect any errors.
Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要以编程方式创建 TableLayout,而是使用 xml 资源文件。
这是关于此的教程: http://huuah.com/using-tablelayout-on-android /
Instead of creating your TableLayout programatically use an xml resource file.
Here is a tutorial on this: http://huuah.com/using-tablelayout-on-android/
我复制了你的代码,这是我得到的错误,还没有答案,但这可能对其他人有帮助
I copied your code, here is the error I got, no answer yet, but this may help others
首先不需要为Drawinterface扩展Activity。当您使用 java 代码创建任何视图时。您需要扩展 View 类或其任何子类。在这里你可以扩展TableLayout。
或者最简单的事情是在 xml 中创建布局。 Java 代码在开发甚至理解过程中都会产生不必要的复杂性(因为它已经在上面的代码中创建了)。
First of all no need to extends Activity for Drawinterface. While you create any View using java code. You need to extend View class or any child class of it. Here you may extend TableLayout.
Or simplest thing is create layout in xml. Java code creates unnecessary complexity while developing and even in understanding (As it is already created in above code).