当我运行我的android应用程序时遇到此错误,有任何建议修复它吗?
10-12 23:05:20.472 29607-29607 / com.example.med E / AndroidRuntime:FATAL 例外:主要 流程:com.example.med,PID:29607 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.med/com.example.med.MainActivity}: java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 在android.app.ActivityThread.access $ 900(ActivityThread.java:174) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1307) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5593) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本机方法) 造成原因:java.lang.NullPointerException 在com.example.med.MainActivity.onCreate(MainActivity.java:104) 在android.app.Activity.performCreate(Activity.java:5458) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 在android.app.ActivityThread.access $ 900(ActivityThread.java:174) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1307) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5593) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本机方法)10-12 23:09:56.627 29899-29899 / com.example.med / AndroidRuntime:FATAL 例外:主要 流程:com.example.med,PID:29899 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.med/com.example.med.MainActivity}: java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 在android.app.ActivityThread.access $ 900(ActivityThread.java:174) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1307) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5593) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本机方法) 造成原因:java.lang.NullPointerException 在com.example.med.MainActivity.onCreate(MainActivity.java:104) 在android.app.Activity.performCreate(Activity.java:5458) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 在android.app.ActivityThread.access $ 900(ActivityThread.java:174) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1307) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5593) 在java.lang.reflect.Method.invokeNative(本机方法) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本机方法)
package com.example.med;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final boolean AUTO_HIDE = true;
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler();
private View mContentView;
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
private View mControlsView;
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
mControlsView.setVisibility(View.VISIBLE);
}
};
private boolean mVisible;
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.performClick();
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
private Button playButton;
private Button lessonButton;
private Button settingsButton;
private Button highscoresButton;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_settings:
startGameSettings();
return true;
case R.id.action_highscores:
startHighScore();
return true;
case R.id.action_playagain:
startGameplay();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_layout_controls);
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle();
}
});
playButton =findViewById(R.id.playButton);
lessonButton = findViewById(R.id.lessonButton);
settingsButton = findViewById(R.id.settingsButton);
highscoresButton = findViewById(R.id.highscoresButton);
playButton.setOnClickListener(this);
lessonButton.setOnClickListener(this);
settingsButton.setOnClickListener(this);
highscoresButton.setOnClickListener(this);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
delayedHide(100);
}
private void toggle() {
if (mVisible) {
hide();
} else {
show();
}
}
private void hide() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mVisible = false;
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
}
@SuppressLint("InlinedApi")
private void show() {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
private void delayedHide(int delayMillis) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, delayMillis);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.playButton){
startGameplay();
}else if (view.getId() == R.id.lessonButton){
startLessonaMenu();
}
else if (view.getId() == R.id.settingsButton){
startGameSettings();
}
else if (view.getId() == R.id.highscoresButton){
startHighScore();
}
}
public void startLessonaMenu() {
Intent intent = new Intent(this, LessonsMenu.class);
startActivity(intent);
}
public void startHighScore() {
Intent intent = new Intent(this, HighScore.class);
startActivity(intent);
}
public void startGameSettings() {
Intent intent = new Intent(this, GameSettings.class);
startActivity(intent);
}
public void startGameplay() {
Intent intent = new Intent(this, Gameplay.class);
startActivity(intent);
}
}
答案 0 :(得分:0)
mContentView
从未分配到任何地方。有你的NPE。
答案 1 :(得分:0)
您必须先使用mContentView
进行初始化:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_layout_controls);
mContentView= findViewById(R.id.mContentView); \\******
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle();
}
});