-2

当我在我的 Nexsus 4 上启动应用程序时,我似乎被迫关闭。很有可能只是我在胡闹,但我找不到代码有什么问题。

Android Studio 没有给出任何错误。被注释掉的重置方法我应该重置复选框和TextView。

为什么我不能在 onCreate 之前定义项目?当我在“重置”方法中无法使用它们之后定义它们时..

有人可以看看吗?

package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {

    final TextView txtTest = (TextView) findViewById(R.id.txtOut);
    final CheckBox chkTest = (CheckBox) findViewById(R.id.chkTest);
    final Button btnTest = (Button) findViewById(R.id.btnTest);
    final EditText etTest = (EditText) findViewById(R.id.etTest);
    String text = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            chkTest.setChecked(true);

            chkTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                            if (chkTest.isChecked()) {
                                    txtTest.setText("Checked :)");
                            } else txtTest.setText("Not Checked");
                    }
            });

            btnTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                            //etTest.getText().toString(text);
                            text = etTest.toString();
                      // Reset(text);
                    }
            });
            /*void Reset(String text) {
                    chkTest.setChecked(false);
                    txtTest.setText(text);
            }*/
    }

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

这是 logcat 错误:

java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{[...].MyActivity}: java.lang.NullPointerException
4

4 回答 4

4

您不能以这种方式初始化项目!执行代码时布局尚未膨胀,final TextView txtTest = (TextView) findViewById(R.id.txtOut);因此您有一个空指针异常。此外,在 onCreate() 之外定义 reset() 方法。请参阅以下代码:

    TextView txtTest = null;
    CheckBox chkTest = null;
    Button btnTest = null;
    EditText etTest = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtTest = (TextView) findViewById(R.id.txtOut);
    chkTest = (CheckBox) findViewById(R.id.chkTest);
    btnTest = (Button) findViewById(R.id.btnTest);
    etTest = (EditText) findViewById(R.id.etTest);

    ....
 }


 void reset(String text) {
      chkTest.setChecked(false);
      txtTest.setText(text);
 }
于 2013-07-18T07:44:13.233 回答
2

尚未创建时找不到视图,请尝试以下操作:

package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {

    TextView txtTest = null;
    CheckBox chkTest = null;
    Button btnTest = null;
    EditText etTest = null;
    String text = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            txtTest = (TextView) findViewById(R.id.txtOut);
            chkTest = (CheckBox) findViewById(R.id.chkTest);
            btnTest = (Button) findViewById(R.id.btnTest);
            etTest = (EditText) findViewById(R.id.etTest);


            chkTest.setChecked(true);

            chkTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                            if (chkTest.isChecked()) {
                                    txtTest.setText("Checked :)");
                            } else txtTest.setText("Not Checked");
                    }
            });

            btnTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                            //etTest.getText().toString(text);
                            text = etTest.toString();
                      // Reset(text);
                    }
            });
    }

    @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;
    }
    private void Reset(String text) {
            chkTest.setChecked(false);
            txtTest.setText(text);
    }

}
于 2013-07-18T07:40:42.813 回答
0

您不能导致您正在初始化的项目在初始化activity_main.xml后设置内容视图。在onCreate().

于 2013-07-18T07:39:56.067 回答
0

首先,您必须Reset()onCreate(). 那不是正确的方法。其次,我观察到你刚刚开始这样做,所以我建议在中定义views然后xml对它们采取行动。您可以在代码中checkbox最初xml设置为 true,然后在代码中设置为 false。

于 2013-07-18T07:45:49.867 回答