2014-03-25 84 views
0

只要此java文件打开,应用程序就会崩溃。请帮助我,我只是在清理应用程序,我做了新班BaconAndEggs,我转移了各种实例,我实施了actionlistener并为这两个按钮制作了单独的开关盒,然后一切正常。Android应用程序无法正常工作,崩溃

package com.example.testing; 

import java.util.Random; 

import android.annotation.TargetApi; 
import android.app.Activity; 
import android.graphics.Color; 
import android.os.Build; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

public class TextPlay extends Activity implements View.OnClickListener{ 

    Button chkCmd; 
    ToggleButton passTog; 
    EditText input; 
    TextView display; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.text); 

     baconAndEggs(); 
     passTog.setOnClickListener(this); 
     chkCmd.setOnClickListener(this); 
    } 
     private void baconAndEggs() { 
     // TODO Auto-generated method stub 
       chkCmd = (Button) findViewById (R.id.bResults); 
       ToggleButton passTog = (ToggleButton) findViewById (R.id.tbPassword); 
       EditText input =(EditText) findViewById (R.id.etCommands); 
       TextView display = (TextView) findViewById (R.id.tvResults); 
      } 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override 
    public void onClick(View view) { 
     // TODO Auto-generated method stub 
     switch(view.getId()){ 
     case R.id.bResults: 
      String check = input.getText().toString(); 
      display.setText(check); 
      if (check.contentEquals("left")){ 
       display.setGravity(Gravity.LEFT); 
       } 
      else if(check.contentEquals("right")){ 
       display.setGravity(Gravity.RIGHT); 
       } 
      else if(check.contentEquals("center")){ 
       display.setGravity(Gravity.CENTER); 
       } 
      else if(check.contentEquals("blue")){ 
       display.setTextColor(Color.BLUE); 
      } 
      else if(check.contains("WTF")){ 
       Random crazy = new Random(); 
       display.setText("WTF!!!!!"); 
       display.setTextSize(crazy.nextInt(75)); 
       display.setTextColor(crazy.nextInt(265)); 
       switch(crazy.nextInt(3)){ 
       case 0: 
        display.setGravity(Gravity.CENTER); 
        break; 
       case 1: 
        display.setGravity(Gravity.RIGHT); 
        break; 
       case 2: 
        display.setGravity(Gravity.LEFT); 
        break; 
       } 
     } 
      else{ 
       display.setText("invalid"); 
       display.setGravity(Gravity.CENTER); 
       display.setTextColor(Color.WHITE); 
      } 

      break; 
      case R.id.tbPassword : 

       if(passTog.isChecked()){ 
         input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_NUMBER_VARIATION_PASSWORD); 
       } 
       else{ 
        input.setInputType(InputType.TYPE_CLASS_TEXT); 
        } 

      break; 

    }  

    } 
} 

错误的logcat我得到是

03-25 14:29:40.928: D/dalvikvm(1047): GC_FOR_ALLOC freed 72K, 5% free 3033K/3180K, paused 114ms, total 122ms 
03-25 14:29:41.048: I/dalvikvm-heap(1047): Grow heap (frag case) to 6.284MB for 3414416-byte allocation 
03-25 14:29:41.148: D/dalvikvm(1047): GC_FOR_ALLOC freed 2K, 3% free 6364K/6516K, paused 98ms, total 98ms 
03-25 14:29:43.218: E/MediaPlayer(1047): Should have subtitle controller already set 
03-25 14:29:43.818: D/gralloc_goldfish(1047): Emulator without GPU emulation detected. 
03-25 14:29:49.388: D/dalvikvm(1047): GC_FOR_ALLOC freed 12K, 2% free 6573K/6660K, paused 132ms, total 141ms 
03-25 14:29:51.628: I/Choreographer(1047): Skipped 50 frames! The application may be doing too much work on its main thread. 
03-25 14:29:54.168: I/Choreographer(1047): Skipped 55 frames! The application may be doing too much work on its main thread. 
03-25 14:29:56.478: D/dalvikvm(1047): GC_FOR_ALLOC freed 18K, 2% free 7782K/7876K, paused 53ms, total 58ms 
03-25 14:29:56.838: D/AndroidRuntime(1047): Shutting down VM 
03-25 14:29:56.838: W/dalvikvm(1047): threadid=1: thread exiting with uncaught exception (group=0xb1a47b90) 
03-25 14:29:56.878: E/AndroidRuntime(1047): FATAL EXCEPTION: main 
03-25 14:29:56.878: E/AndroidRuntime(1047): Process: com.example.testing, PID: 1047 
03-25 14:29:56.878: E/AndroidRuntime(1047): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.TextPlay}: java.lang.NullPointerException 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.os.Handler.dispatchMessage(Handler.java:102) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.os.Looper.loop(Looper.java:137) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at dalvik.system.NativeStart.main(Native Method) 
03-25 14:29:56.878: E/AndroidRuntime(1047): Caused by: java.lang.NullPointerException 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at com.example.testing.TextPlay.onCreate(TextPlay.java:29) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.Activity.performCreate(Activity.java:5243) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
03-25 14:29:56.878: E/AndroidRuntime(1047):  ... 11 more 
03-25 14:30:05.018: I/Process(1047): Sending signal. PID: 1047 SIG: 9 
03-25 14:32:29.158: D/dalvikvm(1104): GC_FOR_ALLOC freed 35K, 4% free 3033K/3144K, paused 43ms, total 48ms 
03-25 14:32:29.188: I/dalvikvm-heap(1104): Grow heap (frag case) to 6.284MB for 3414416-byte allocation 
03-25 14:32:29.238: D/dalvikvm(1104): GC_FOR_ALLOC freed 2K, 2% free 6364K/6480K, paused 44ms, total 44ms 
03-25 14:32:29.708: E/MediaPlayer(1104): Should have subtitle controller already set 
03-25 14:32:30.028: D/gralloc_goldfish(1104): Emulator without GPU emulation detected. 
03-25 14:32:35.048: D/dalvikvm(1104): GC_FOR_ALLOC freed 13K, 2% free 6575K/6660K, paused 73ms, total 75ms 
03-25 14:32:35.828: I/Choreographer(1104): Skipped 38 frames! The application may be doing too much work on its main thread. 
03-25 14:32:42.318: D/dalvikvm(1104): GC_FOR_ALLOC freed 18K, 2% free 7784K/7876K, paused 53ms, total 66ms 
03-25 14:32:55.058: D/AndroidRuntime(1104): Shutting down VM 
03-25 14:32:55.058: W/dalvikvm(1104): threadid=1: thread exiting with uncaught exception (group=0xb1a47b90) 
03-25 14:32:55.118: E/AndroidRuntime(1104): FATAL EXCEPTION: main 
03-25 14:32:55.118: E/AndroidRuntime(1104): Process: com.example.testing, PID: 1104 
03-25 14:32:55.118: E/AndroidRuntime(1104): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.TextPlay}: java.lang.NullPointerException 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.os.Handler.dispatchMessage(Handler.java:102) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.os.Looper.loop(Looper.java:137) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at dalvik.system.NativeStart.main(Native Method) 
03-25 14:32:55.118: E/AndroidRuntime(1104): Caused by: java.lang.NullPointerException 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at com.example.testing.TextPlay.onCreate(TextPlay.java:31) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.Activity.performCreate(Activity.java:5243) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
03-25 14:32:55.118: E/AndroidRuntime(1104):  ... 11 more 
03-25 14:32:59.768: I/Process(1104): Sending signal. PID: 1104 SIG: 9 
+0

03-25 14:29:56.878:E/AndroidRuntime(1047):产生的原因:显示java.lang.NullPointerException 03 -25 14:29:56.878:E/AndroidRuntime(1047):在com.example.testing.TextPlay.onCreate(TextPlay.java:29)可以指定哪一行是29行吗? –

+0

为什么你需要'baconAndEggs'方法? –

+0

如果你可以看看我以前被搁置的问题,但我编辑过,但没有人回答然后http://stackoverflow.com/questions/22637218/small-portion-of-code-not-working – user3458343

回答

2

这是一个范围问题。

里面baconAndEggs()申报并分配一些变量:

ToggleButton passTog = (ToggleButton) findViewById (R.id.tbPassword); 
EditText input =(EditText) findViewById (R.id.etCommands); 
TextView display = (TextView) findViewById (R.id.tvResults); 

一旦baconAndEggs()退出,这些变量不再存在。

考虑到你宣布所有这些为类的成员,你可能是指:

passTog = (ToggleButton) findViewById (R.id.tbPassword); 
input =(EditText) findViewById (R.id.etCommands); 
display = (TextView) findViewById (R.id.tvResults); 
+0

它工作的人... thanx ........ – user3458343

+0

随时随地。快乐的编码! :D –

+0

plz看到这个问题,如果不是很忙.... http://stackoverflow.com/questions/22637218/small-portion-of-code-not-working – user3458343

1

您有:

ToggleButton passTog = (ToggleButton) findViewById (R.id.tbPassword); 
在功能 baconAndEggs

。使那

passTog = (ToggleButton) findViewById (R.id.tbPassword); 

否则它成为一个局部变量。

+0

你是正确的...但同样的问题是与输入和diplay .... thanxx – user3458343

相关问题