我是android开发新手。我的新应用程序崩溃在我的手机中(我使用手机而不是虚拟机)但代码是正确的请帮助
05-16 17:36:52.717 28823-28823/ebin.namez I/art: Late-enabling -Xcheck:jni
05-16 17:36:52.728 27804-27886/? I/PBSessionCacheImpl: Deleted sessionId[122284780131059156] from persistence.
05-16 17:36:52.778 28823-28823/ebin.namez W/System: ClassLoader referenced unknown path: /data/app/ebin.namez-2/lib/arm64
05-16 17:36:52.800 28823-28823/ebin.namez D/AndroidRuntime: Shutting down VM
05-16 17:36:52.801 28823-28823/ebin.namez E/AndroidRuntime: FATAL EXCEPTION: main
Process: ebin.namez, PID: 28823
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ebin.namez/ebin.namez.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at ebin.namez.MainActivity.<init>(MainActivity.java:36)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2573)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2742)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1489)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) `
以下是MainActivity
package ebin.namez;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "nothing", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
TextView op;
{
op=(TextView)findViewById(R.id.op);
}
Button button;
{
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op.setText("hey");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是一个简单的程序,当按下按钮时显示“嘿”
答案 0 :(得分:1)
错误显示您在ebin.namez.MainActivity.<init>(MainActivity.java:36)
处获得了NullPointerExeception。这是initializer block中的行op=(TextView)findViewById(R.id.op)
- 如果您以1:1发布该代码。
在正确设置Activity之前,将在创建实例时执行此块。 Activity是一个组件,它的生命周期以onCreate
开头,因此在实例创建时访问组件属性和方法(如findViewById
)是一个非常糟糕的主意。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
// Member op
TextView op;
// initializer block: executed on instance creation
{
op=(TextView)findViewById(R.id.op); // Line 36
}
...
}
您可能希望op
方法中的button
和onCreate
代码如此:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// local variable op, declared final for use in anonymous class method
final TextView op = (TextView)findViewById(R.id.op);
// local variable button
Button button;
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op.setText("hey");
}
});
}