Android 运行应用程序时无法获取要加载的视图

Android 运行应用程序时无法获取要加载的视图,android,xml,view,android-layout,Android,Xml,View,Android Layout,我正在为android编写我的第一个应用程序,我无法加载xml布局。我看了参考资料,似乎找不到问题所在。代码编译并运行,但屏幕上显示的只是一个黑色页面,状态和标题栏位于顶部 有什么想法吗 public void OnCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View cancelButton= findVie

我正在为android编写我的第一个应用程序,我无法加载xml布局。我看了参考资料,似乎找不到问题所在。代码编译并运行,但屏幕上显示的只是一个黑色页面,状态和标题栏位于顶部

有什么想法吗

public void OnCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    View cancelButton= findViewById(R.id.button1);
    cancelButton.setOnClickListener(this);
    View sevenButton= findViewById(R.id.button2);
    sevenButton.setOnClickListener(this);
    View fourButton= findViewById(R.id.button3);
    fourButton.setOnClickListener(this);
    View oneButton= findViewById(R.id.button4);
    oneButton.setOnClickListener(this);
    View zeroButton= findViewById(R.id.button5);
    zeroButton.setOnClickListener(this);
    View divideButton= findViewById(R.id.button6);
    divideButton.setOnClickListener(this);
    View eightButton= findViewById(R.id.button7);
    eightButton.setOnClickListener(this);
    View fiveButton= findViewById(R.id.button8);
    fiveButton.setOnClickListener(this);
    View twoButton= findViewById(R.id.button9);
    twoButton.setOnClickListener(this);
    View pointButton= findViewById(R.id.button10);
    pointButton.setOnClickListener(this);
    View timesButton= findViewById(R.id.button11);
    timesButton.setOnClickListener(this);
    View nineButton= findViewById(R.id.button12);
    nineButton.setOnClickListener(this);
    View sixButton= findViewById(R.id.button13);
    sixButton.setOnClickListener(this);
    View threeButton= findViewById(R.id.button14);
    threeButton.setOnClickListener(this);
    View shiftButton= findViewById(R.id.button15);
    shiftButton.setOnClickListener(this);
    View backButton= findViewById(R.id.button16);
    backButton.setOnClickListener(this);
    View addButton= findViewById(R.id.button17);
    addButton.setOnClickListener(this);
    View minusButton= findViewById(R.id.button18);
    minusButton.setOnClickListener(this);
    View equalsButton= findViewById(R.id.button19);
    equalsButton.setOnClickListener(this);


}
更新 下面是xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/main" 
             android:layout_height="fill_parent" 
             xmlns:android="http://schemas.android.com/apk/res/android" 
             android:layout_width="fill_parent" 
             android:background="@drawable/trondroid">
    <TableRow android:layout_height="wrap_content" 
              android:id="@+id/tableRow1" 
              android:layout_width="wrap_content" 
              android:layout_gravity="center|center_horizontal" 
              android:layout_marginLeft="6dip" 
              android:layout_marginTop="100dip">
        <LinearLayout android:orientation="vertical" 
                      android:id="@+id/linearLayout1" 
                      android:layout_width="76dip" 
                      android:layout_height="wrap_content">
            <Button android:text="c" 
                    android:id="@+id/button1" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="7" 
                    android:id="@+id/button2" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="4" 
                    android:id="@+id/button3" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="1" 
                    android:id="@+id/button4" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="0" 
                    android:id="@+id/button5" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout2" 
                      android:orientation="vertical" 
                      android:layout_height="match_parent" 
                      android:layout_width="75dip">
            <Button android:text="/" 
                    android:id="@+id/button6" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="8" 
                    android:id="@+id/button7" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="5" 
                    android:id="@+id/button8" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="2" 
                    android:id="@+id/button9" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="." 
                    android:id="@+id/button10" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout3" 
                      android:layout_height="match_parent" 
                      android:orientation="vertical" 
                      android:layout_width="75dip">
            <Button android:text="*" 
                    android:id="@+id/button11" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="9" 
                    android:id="@+id/button13" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="6" 
                    android:id="@+id/button14" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="3" 
                    android:id="@+id/button15" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="" 
                    android:id="@+id/button16" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
        </LinearLayout>
        <LinearLayout android:layout_height="match_parent" 
                      android:id="@+id/linearLayout4" 
                      android:orientation="vertical" 
                      android:layout_width="75dip">
            <Button android:text="" 
                    android:id="@+id/button12" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="+" 
                    android:id="@+id/button17" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="-" 
                    android:id="@+id/button18" 
                    android:layout_width="match_parent" 
                    android:layout_height="75dip">
            </Button>
            <Button android:text="=" 
                    android:id="@+id/button19" 
                    android:layout_height="match_parent" 
                    android:layout_width="match_parent">
            </Button>
        </LinearLayout>
    </TableRow>
</TableLayout>

嘿,这里:)对于OnClickListener,您实际上必须提供OnClickListener实现。您正在传递“this”,这是否意味着您的活动实现了View.OnClickListener?因为如果没有,上面的代码将无法工作。但是还有一件事:无论是否设置了OnClickListener,您的布局(如果您在XML中定义了它)都应该显示出来。您可以将您的XML布局发布到吗

干杯, Ready4Android


另外,如果这真的是你的第一个应用程序,你可能想先从一些教程开始:)你会学到很多基本的东西,这将帮助你入门。官方网站是一个很好的入门网站。

删除代码,直到它开始工作。然后开始把东西一个接一个地放回去,直到它破裂。简单。我已经实现了OnClickListener,并且发布了xml文件。