如何回答下面的logcat?

时间:2013-11-26 13:53:39

标签: android

有人在这里请帮我查一下这个LogCat,究竟是什么错误?点击按钮后,它总是出现错误,事件我试图多次修复它。

11-26 20:49:24.143: W/dalvikvm(436): threadid=1: thread exiting with uncaught exception (group=0x40014760)
11-26 20:49:24.173: E/AndroidRuntime(436): FATAL EXCEPTION: main
11-26 20:49:24.173: E/AndroidRuntime(436): java.lang.RuntimeException: Unable to start activity ComponentInfo{kids.iq.kidsiqpicturesquestion/kids.iq.kidsiqpicturesquestion.animal_q_easy_3}: java.lang.NullPointerException
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.os.Looper.loop(Looper.java:132)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread.main(ActivityThread.java:4123)
11-26 20:49:24.173: E/AndroidRuntime(436):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 20:49:24.173: E/AndroidRuntime(436):  at java.lang.reflect.Method.invoke(Method.java:491)
11-26 20:49:24.173: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-26 20:49:24.173: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-26 20:49:24.173: E/AndroidRuntime(436):  at dalvik.system.NativeStart.main(Native Method)
11-26 20:49:24.173: E/AndroidRuntime(436): Caused by: java.lang.NullPointerException
11-26 20:49:24.173: E/AndroidRuntime(436):  at kids.iq.kidsiqpicturesquestion.animal_q_easy_3.onCreate(animal_q_easy_3.java:75)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.Activity.performCreate(Activity.java:4397)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
11-26 20:49:24.173: E/AndroidRuntime(436):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
11-26 20:49:24.173: E/AndroidRuntime(436):  ... 11 more

我的活动

public class animal_q_easy_3 extends Activity {

//SQLite Method
private SQLiteAdapter mySQLiteAdapter;

private ImageView play_home, play_now, play_again;

private TextView get_text_1, get_text_2, title, top_answer_ab;

private TextView score = null;
private int correct = 0;
private CountDownTimer aCounter = null;

TextView timer, l_timer;

private Button tiger, chicken, bee, pig;
private Button tiger_t, chicken_t, bee_t, pig_t;

private TextView ti, ch, be, pi;
private TextView ti_txt, ch_txt, be_txt, pi_txt;

Dialog myDialog;

@SuppressLint({ "NewApi", "SimpleDateFormat" })
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    Random rnd = new Random();
    int screen = (int) rnd.nextInt(3)+1;

    switch(screen)
    {
        case 1:
            setContentView(R.layout.animal_q_easy_3);
        break;
        case 2:
            setContentView(R.layout.animal_q_easy_3_a);
        break;
        case 3:
            setContentView(R.layout.animal_q_easy_3_b);
        break;
    }

    ti = (TextView) findViewById(R.id.ti); ti.setVisibility(View.GONE);
    ch = (TextView) findViewById(R.id.ch); ch.setVisibility(View.GONE);
    be = (TextView) findViewById(R.id.be); be.setVisibility(View.GONE);
    pi = (TextView) findViewById(R.id.pi); pi.setVisibility(View.GONE);

    ti_txt = (TextView) findViewById(R.id.ti_txt); ti_txt.setVisibility(View.GONE);
    ch_txt = (TextView) findViewById(R.id.ch_txt); ch_txt.setVisibility(View.GONE);
    be_txt = (TextView) findViewById(R.id.be_txt); be_txt.setVisibility(View.GONE);
    pi_txt = (TextView) findViewById(R.id.pi_txt); pi_txt.setVisibility(View.GONE);

    //Hide Play Again, Go Home Button
    final RelativeLayout top_answer = (RelativeLayout) findViewById(R.id.top_answer);
    //top_answer.setVisibility(View.GONE);
    final RelativeLayout gameEnd = (RelativeLayout) findViewById(R.id.game_ended);
    gameEnd.setVisibility(View.GONE);
    play_home = (ImageView) findViewById(R.id.play_home);
    play_home.setVisibility(View.GONE);
    play_again = (ImageView) findViewById(R.id.play_again);
    play_again.setVisibility(View.GONE);
    play_now = (ImageView) findViewById(R.id.play_now);
    play_now.setVisibility(View.GONE);

    final MediaPlayer failAnswer = MediaPlayer.create(this, R.raw.fail_buzzer);
    final MediaPlayer welldone = MediaPlayer.create(this, R.raw.welldone);
    final MediaPlayer yousosmart = MediaPlayer.create(this, R.raw.yousosmart);
    final MediaPlayer yousogood = MediaPlayer.create(this, R.raw.yousogood);
    final MediaPlayer win = MediaPlayer.create(this, R.raw.win_sound);
    final MediaPlayer ohmygod = MediaPlayer.create(this, R.raw.ohmygod);

    title = (TextView) findViewById(R.id.title);
    top_answer_ab = (TextView) findViewById(R.id.tittop_answer_ab);
    top_answer_ab.setVisibility(View.GONE);

    get_text_1 = (TextView) findViewById(R.id.get_text_1);
    get_text_1.setTextColor(Color.argb(0, 255, 0, 0));
    get_text_2 = (TextView) findViewById(R.id.get_text_2);
    get_text_2.setTextColor(Color.argb(0, 255, 0, 0));
    score = (TextView) findViewById(R.id.score);
    score.setTextColor(Color.argb(0, 255, 0, 0));

    tiger = (Button) findViewById(R.id.tiger);
    tiger.setTextColor(Color.argb(0, 255, 0, 0));
    tiger.setBackgroundResource(R.drawable.memory_cover);

    chicken = (Button) findViewById(R.id.chicken);
    chicken.setTextColor(Color.argb(0, 255, 0, 0));
    chicken.setBackgroundResource(R.drawable.memory_cover);

    bee = (Button) findViewById(R.id.bee);
    bee.setTextColor(Color.argb(0, 255, 0, 0));
    bee.setBackgroundResource(R.drawable.memory_cover);

    pig = (Button) findViewById(R.id.pig);
    pig.setTextColor(Color.argb(0, 255, 0, 0));
    pig.setBackgroundResource(R.drawable.memory_cover);

    tiger_t = (Button) findViewById(R.id.tiger_t);
    tiger_t.setTextColor(Color.argb(0, 255, 0, 0));
    tiger_t.setBackgroundResource(R.drawable.memory_cover);

    chicken_t = (Button) findViewById(R.id.chicken_t);
    chicken_t.setTextColor(Color.argb(0, 255, 0, 0));
    chicken_t.setBackgroundResource(R.drawable.memory_cover);

    bee_t = (Button) findViewById(R.id.bee_t);
    bee_t.setTextColor(Color.argb(0, 255, 0, 0));
    bee_t.setBackgroundResource(R.drawable.memory_cover);

    pig_t = (Button) findViewById(R.id.pig_t);
    pig_t.setTextColor(Color.argb(0, 255, 0, 0));
    pig_t.setBackgroundResource(R.drawable.memory_cover);

  //Timer
    timer = (TextView) findViewById(R.id.timer);
    l_timer = (TextView) findViewById(R.id.l_timer);
    aCounter = new CountDownTimer(32000, 1000)
    {
        public void onTick(long millisUntilFinished)
        {
            timer.setText("" +new SimpleDateFormat("ss").format(new Date(millisUntilFinished)));

            if((millisUntilFinished / 1000)<=10 )
            {
                timer.setTextColor(Color.RED);
                failAnswer.start();
            }
        }

        public void onFinish()
        {
            ohmygod.start();
            timer.setVisibility(View.GONE);
            l_timer.setVisibility(View.GONE);
            title.setVisibility(View.GONE);

            RelativeLayout gameEnd = (RelativeLayout) findViewById(R.id.game_ended);
            gameEnd.setVisibility(View.VISIBLE);
            play_home.setVisibility(View.VISIBLE);
            play_again.setVisibility(View.VISIBLE);

            tiger.setVisibility(View.GONE); tiger_t.setVisibility(View.GONE);
            chicken.setVisibility(View.GONE); chicken_t.setVisibility(View.GONE);
            bee.setVisibility(View.GONE); bee_t.setVisibility(View.GONE);
            pig.setVisibility(View.GONE); pig_t.setVisibility(View.GONE);

            top_answer_ab.setVisibility(View.VISIBLE);
            top_answer_ab.setTextColor(Color.RED);
            top_answer_ab.setText("Time's up! You failed!");
            //Insert into DB
            mySQLiteAdapter = new SQLiteAdapter(animal_q_easy_3.this);
            mySQLiteAdapter.openToWrite();

            // Data del trov insert
            String level = "Easy";
            String failed = "Fail";
            int A = 0;
            int B = 0;
            int level_step_b = 0;
            int percentage = 0;

            Calendar c = Calendar.getInstance();
            System.out.println("Current time => " + c.getTime());
            SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm");
            String formattedDate = df.format(c.getTime());

            mySQLiteAdapter.insert_ANIMAL(level, A, B, formattedDate, percentage, failed, level_step_b);

            mySQLiteAdapter.close();
        }
     };
     aCounter.start();

2 个答案:

答案 0 :(得分:1)

这是你的错误

11-26 20:49:24.173: E/AndroidRuntime(436): Caused by: java.lang.NullPointerException 11-26 20:49:24.173: E/AndroidRuntime(436): at kids.iq.kidsiqpicturesquestion.animal_q_easy_3.onCreate(animal_q_easy_3.java:75)

75此处表示发生错误的行号。请发布该行的代码,并且将来始终在包含caused by的日志中搜索此类型的行,然后是您的Class名称。

答案 1 :(得分:1)

最有可能的是,你的id不是根据setContentview(基于switch case语句),所以当你调用时

ti = (TextView) findViewById(R.id.ti); ti.setVisibility(View.GONE);

无法识别R.id.ti,因此无法识别NullPointerException

根据从switch声明

中选择的布局检查您是否正在调用正确的ID