gpt4 book ai didi

android - android应用在启动时崩溃

转载 作者:行者123 更新时间:2023-12-03 12:04:42 25 4
gpt4 key购买 nike

我正在制作一个简单的计算器,并在运行时通过java动态添加按钮

我的Java文件是

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

Button [] button = new Button[17];
TextView input_field = new TextView(null);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setlayout();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private void setlayout(){

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int x = 4;
height -= x ;
input_field.setHeight(x);
input_field.setWidth((width-x));
height /= 4;
width /= 4;
for(int i=0;i<16;i++)
{
button [i] = new Button(null);
button[i].setHeight(height);
button[i].setWidth(width);
}
button [16] = new Button(null);
button[16].setHeight(x);
button[16].setWidth(x);

LinearLayout top = new LinearLayout(null);
top.addView(input_field);
top.addView(button[16]);

LinearLayout row1 = new LinearLayout(null);
row1.addView(button[8]);
row1.addView(button[9]);
row1.addView(button[10]);
row1.addView(button[12]);

LinearLayout row2 = new LinearLayout(null);
row2.addView(button[7]);
row2.addView(button[6]);
row2.addView(button[5]);
row2.addView(button[13]);

LinearLayout row3 = new LinearLayout(null);
row3.addView(button[4]);
row3.addView(button[3]);
row3.addView(button[2]);
row3.addView(button[14]);

LinearLayout row4 = new LinearLayout(null);
row4.addView(button[0]);
row4.addView(button[1]);
row4.addView(button[11]);
row4.addView(button[15]);

LinearLayout main = (LinearLayout) findViewById(R.id.main_layout);

main.setVisibility(1);
main.setOrientation(1);
main.addView(top);
main.addView(row1);
main.addView(row2);
main.addView(row3);
main.addView(row4);

main.setVisibility(0);
}
}

这是我的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >


</LinearLayout>

这是我的logCat错误
07-22 12:58:42.725: E/AndroidRuntime(29430): FATAL EXCEPTION: main
07-22 12:58:42.725: E/AndroidRuntime(29430): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2675)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2117)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.os.Looper.loop(Looper.java:143)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread.main(ActivityThread.java:4717)
07-22 12:58:42.725: E/AndroidRuntime(29430): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430): at java.lang.reflect.Method.invoke(Method.java:521)
07-22 12:58:42.725: E/AndroidRuntime(29430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-22 12:58:42.725: E/AndroidRuntime(29430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-22 12:58:42.725: E/AndroidRuntime(29430): at dalvik.system.NativeStart.main(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430): Caused by: java.lang.NullPointerException
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.view.ViewConfiguration.get(ViewConfiguration.java:216)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.view.View.<init>(View.java:1826)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.view.View.<init>(View.java:1873)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.widget.TextView.<init>(TextView.java:432)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.widget.TextView.<init>(TextView.java:426)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.widget.TextView.<init>(TextView.java:421)
07-22 12:58:42.725: E/AndroidRuntime(29430): at com.example.calculator.MainActivity.<init>(MainActivity.java:12)
07-22 12:58:42.725: E/AndroidRuntime(29430): at java.lang.Class.newInstanceImpl(Native Method)
07-22 12:58:42.725: E/AndroidRuntime(29430): at java.lang.Class.newInstance(Class.java:1429)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-22 12:58:42.725: E/AndroidRuntime(29430): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2667)
07-22 12:58:42.725: E/AndroidRuntime(29430): ... 11 more

最佳答案

为什么将null传递给LinearLayout?它要求您向其传递 Activity 的上下文(在您的情况下为this)。按钮和您选择在代码中动态创建的任何其他 View 也是如此。

另外,看看这个答案,因为我怀疑您用this替换所有空值后就会遇到另一个问题,这就是您的 View 将不知道要宽或高的问题。
Set margins in a LinearLayout programmatically

关于android - android应用在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com