1

When I ran the app the first activity worked but as soon as it went to the second activity it said treasure hunt has stopped working, and there was nothing under logcat or console. I am pretty sure it is a problem with level_1.java but can't figure out what. Please help thanks.

Main Activity:

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>

Manifest:

<?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>
4

1 回答 1

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;
                }
      }

}
于 2013-10-11T03:30:02.927 回答