简体   繁体   English

Android App开始崩溃

[英]Android App crashing at beginning

I'm learning Android development and I'm wondering why is my app crashing 我正在学习Android开发,想知道为什么我的应用崩溃了

Main code: 主要代码:

package com.tester.myapp;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class Main extends Activity {

    Button carrega;
    TextView texto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        carrega = (Button)findViewById(R.id.carregar);
        texto = (TextView)findViewById(R.id.textView3);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        carrega.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                texto.setText("newtext");
            }
        });
    }

    @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;
    }

}

And my XML is: 我的XML是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:background="#A5BB76"
        android:text="@string/message" />
    <Button
        android:id="@+id/carregar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="@string/OK"
    />

</LinearLayout>

And this app works OK just by the XML, but if I define those variables (Button and TextView) it crashes at start. 而且该应用程序仅通过XML即可正常工作,但是如果我定义了这些变量(Button和TextView),则它在启动时会崩溃。 Why is this happening? 为什么会这样呢? How can I solve this? 我该如何解决? If I take off my Java new code (and maintain the default) the app works, but it doesn't whenever I declare one of those variables. 如果我摘下Java新代码(并保持默认代码),则该应用程序将运行,但无论何时声明这些变量之一,它都不会起作用。

I appreciate your time. 感谢您的宝贵时间。 Thank you very much. 非常感谢你。 Any help would be great. 任何帮助都会很棒。

You should Initialise the View after the setContentView(R.layout.activity_main); 您应该在setContentView(R.layout.activity_main);之后初始化视图setContentView(R.layout.activity_main);

Change It 更改

@Override
    protected void onCreate(Bundle savedInstanceState) {
        carrega = (Button)findViewById(R.id.carregar);
        texto = (TextView)findViewById(R.id.textView3);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

To

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        carrega = (Button)findViewById(R.id.carregar);
        texto = (TextView)findViewById(R.id.textView3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM