2013-10-11 191 views
1

當我運行應用程序時,第一個活動起作用,但一旦它進入第二個活動,它說尋寶已停止工作,並且logcat或控制檯下沒有任何東西。我很確定這是一個level_1.java的問題,但無法弄清楚什麼。請幫忙謝謝。尋寶已停止工作

主要活動:

package com.example.treasurehunt; 


import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    public void Start(View view) { 
     Log.d("hi", "hello"); 
     Intent intent = new Intent(this, Level_1.class); 
     startActivity(intent);  
     finish(); 
    } 

} 

Mainxml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Treasure Hunt" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="82dp" 
     android:text="Start" 
     android:onClick="Start" /> 

</RelativeLayout> 

`

level_1java:

package com.example.treasurehunt; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.SharedPreferences; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Level_1 extends Activity { 
    private SharedPreferences settings; 
    private SharedPreferences.Editor editor; 
    TextView Clue = (TextView)findViewById(R.id.Clue); 
    EditText edittext = (EditText)findViewById(R.id.editText); 
    String hint; 
    String answer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d("hi","hello"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_level_1); 
     int level = settings.getInt("level", 1); 

      switch (level) { 
       case 1: level1(); 
         break; 
       default: 
         break; 
      } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.level_1, menu); 
     return true; 
    } 

    public void level1() { 
     editor.putInt("level", 1); // only needs to be done for level 1 
     editor.commit(); 
     Clue.setText("Bartholdi was my creator, I carry fire and knowledge, what am I?"); 
     hint = "July IV MDCCLXXVI"; 
     answer = "statue of liberty"; 
    } 
    public void answer() {  
     String useranswer = edittext.toString(); 
     if (useranswer.equalsIgnoreCase(answer)) {// if they get the correct answer 
      int leveltest = settings.getInt("level", 1); 
      editor.putInt("level", leveltest+1); //adds one level to the current level 
      editor.commit(); 
     } 

    } 
    public void hint() { //function that displays the hint 

    } 

} 

LEVEL_1 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Level_1" > 

    <TextView 
     android:id="@+id/Clue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="116dp" 
     android:text="Clue" /> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/Clue" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="80dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/Submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/editText" 
     android:layout_marginBottom="50dp" 
     android:text="Submit" 
     android:onClick="answer"/> 

    <Button 
     android:id="@+id/Hint" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/Submit" 
     android:layout_alignBottom="@+id/Submit" 
     android:layout_alignLeft="@+id/editText" 
     android:text="Hint" 
     android:onClick="Hint"/> 

</RelativeLayout> 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.treasurehunt" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.treasurehunt.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.treasurehunt.Level_1" 
      android:label="@string/title_activity_level_1" > 
     </activity> 
    </application> 

</manifest> 
+1

你能後的logcat的輸出? – ashatte

+0

我的日誌貓不顯示任何東西 – koochi10

+1

哦對了,對不起,你確實提到了... – ashatte

回答

2

您似乎遇到NullPointerException

在字段聲明

TextView Clue = (TextView)findViewById(R.id.Clue); 
EditText edittext = (EditText)findViewById(R.id.editText); 

移動的分配到onCreate()

public class Level_1 extends Activity 
{ 

     // Other members .... 

    TextView Clue = (TextView)findViewById(R.id.Clue); 
    EditText edittext = (EditText)findViewById(R.id.editText); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      Log.d("hi","hello"); 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_level_1); 

      // Assignemnts should be here 
      Clue = (TextView)findViewById(R.id.Clue); 
      edittext = (EditText)findViewById(R.id.editText); 

      int level = settings.getInt("level", 1); 

       switch (level) { 
        case 1: level1(); 
          break; 
        default: 
          break; 
       } 
     } 

} 
+0

感謝超級有用:) – koochi10

+0

如果這解決了你的問題,請按下檢查按鈕來標記答案:) – Geros