38行目のTextView mTextView = new TextView(this);でエラーが起きているようです。表示する段階でnullならエラーが出るのがまだ分かるですが、なぜインスタンス化する段階でこのようなエラーが出るのでしょうか。そして解決するにはどのようにしたらいいのでしょうか?
java
1 2public class MainActivity extends Activity implements Runnable, SensorEventListener { 3 private static final float ACCEL_WEIGHT = 3f; 4 SensorManager manager; 5 Ball ball; 6 EnemyBall eball1; 7 EnemyBall eball2; 8 Handler handler; 9 Handler mHandler; 10 int width, height, time; 11 float gx, gy, dpi; 12 int mLaptime = 0; 13 Timer mTimer = new Timer(true); 1438. TextView mTextView = new TextView(this); 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 getWindow().addFlags( 20 WindowManager.LayoutParams.FLAG_FULLSCREEN); 21 requestWindowFeature(Window.FEATURE_NO_TITLE); 22 FrameLayout framelayout = new FrameLayout(this); 23 framelayout.setBackgroundColor(Color.WHITE); 24 setContentView(framelayout); 25 time = 1; 26 handler = new Handler(); 27 mHandler = new Handler(); 28 dpi = getResources().getDisplayMetrics().densityDpi; 29 30 framelayout.addView(mTextView); 31 mTimer.schedule( new TimerTask(){ 32 @Override 33 public void run() { 34 // mHandlerを通じてUI Threadへ処理をキューイング 35 mHandler.post( new Runnable() { 36 public void run() { 37 38 //実行間隔分を加算処理 39 mLaptime += 1; 40 41 //計算にゆらぎがあるので小数点第1位で丸める 42 BigDecimal bi = new BigDecimal(mLaptime); 43 float outputValue = bi.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); 44 45 //現在のLapTime 46 mTextView.setText(Float.toString(mLaptime)); 47 } 48 }); 49 } 50 }, 0, 1000); 51 52
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.access$900(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.content.ContextWrapper.getResources(ContextWrapper.java:87) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) at android.view.View.<init>(View.java:3784) at android.view.View.<init>(View.java:3898) at android.widget.TextView.<init>(TextView.java:677) at android.widget.TextView.<init>(TextView.java:671) at android.widget.TextView.<init>(TextView.java:667) at android.widget.TextView.<init>(TextView.java:663) at com.example.k15102kk.ball.MainActivity.<init>(MainActivity.java:38) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.access$900(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/05 06:26