即时出现此错误,当我使用其他类的findviewbyid时,似乎会出现。
MainActivity:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button boton=(Button)findViewById(R.id.button);
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText usuario=(EditText)findViewById(R.id.editText);
usuario1 =(EditText)findViewById(R.id.editText);
EditText password = (EditText) findViewById(R.id.editText2);
String user = usuario1.getText().toString();
String pass = password.getText().toString();
Esta asd=new Esta();
asd.function();
}
});}
Esta.class:
package com.example.sori.facultad_if;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
public class Esta extends AppCompatActivity{
public void function() {
setContentView(R.layout.activity_main);
EditText thisone=(EditText)findViewById(R.id.editText);
}}
错误记录:
03-05 22:44:12.061 10088-10088 / com.example.sori.facultad_if E / AndroidRuntime:致命异常:主要 处理:com.example.sori.facultad_if,PID:10088 java.lang.NullPointerException:尝试调用虚方法' android.view.Window $ Callback android.view.Window.getCallback()'在...上 null对象引用
提前致谢
答案 0 :(得分:0)
试试这样:
MainActivity:
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText usuario=(EditText)findViewById(R.id.editText);
EditText password = (EditText) findViewById(R.id.editText2);
Button boton=(Button)findViewById(R.id.button);
boton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String user = usuario1.getText().toString();
String pass = password.getText().toString();
startActivity(new Intent(activity, ImageEditingActivity.class).putExtra("user", user).putExtra("pass", pass));
}
});
}
Esta.class:
package com.example.sori.facultad_if;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
public class Esta extends AppCompatActivity{
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//noinspection ConstantConditions
String user = getIntent().getExtras().getString("user", "");
String pass = getIntent().getExtras().getString("pass", "");
} catch (Exception e) {
e.printStackTrace();
}
EditText thisone=(EditText)findViewById(R.id.editText);
}
}