0

I am getting this error when I am trying to run my app..

please help me :)

my code in the activity that trying to open the Ajax activity:

    package com.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class Champions extends Activity{

    ImageButton ajax,arsenal,barca,bayern,chelsea,cska,dortmund,gala,inter,juve,leverkusen,milan,porto,psg,psv,real,schalke,united,valencia,zenit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.champions);
        initialize();
        ajax.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent ajax1=new Intent(Champions.this,Ajax.class);
                startActivity(ajax1);
            }
        });
    }

    private void initialize() {
        // TODO Auto-generated method stub
        ajax = (ImageButton) findViewById(R.id.IBajax);
        arsenal = (ImageButton) findViewById(R.id.IBarsenal);
        barca = (ImageButton) findViewById(R.id.IBbarca);
        bayern = (ImageButton) findViewById(R.id.IBbayern);
        chelsea = (ImageButton) findViewById(R.id.IBchelsea);
        cska = (ImageButton) findViewById(R.id.IBcska);
        dortmund = (ImageButton) findViewById(R.id.IBdortmund);
        gala = (ImageButton) findViewById(R.id.IBgala);
        inter = (ImageButton) findViewById(R.id.IBinter);
        juve = (ImageButton) findViewById(R.id.IBjuve);
        leverkusen = (ImageButton) findViewById(R.id.IBleverkusen);
        milan = (ImageButton) findViewById(R.id.IBmilan);
        porto = (ImageButton) findViewById(R.id.IBporto);
        psg = (ImageButton) findViewById(R.id.IBpsg);
        psv = (ImageButton) findViewById(R.id.IBpsv);
        real = (ImageButton) findViewById(R.id.IBreal);
        schalke = (ImageButton) findViewById(R.id.IBschalke);
        united = (ImageButton) findViewById(R.id.IBunited);
        valencia = (ImageButton) findViewById(R.id.IBvalencia);
        zenit = (ImageButton) findViewById(R.id.IBzenit);
    }

}

and my manifest:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.app.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.app.Play"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.app.Champions"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            </activity>
        <activity
            android:name="com.app.Ajax"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.actionAJAX" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
            <activity
            android:name="com.app.Stats"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

what do I need to do for the app will run?

Ajax.java:

    package com.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Ajax extends Activity{

    TextView TVajax;
    Context context = getApplicationContext();
    Button hint1,hint2,check,used,right;
    EditText answer; 
    Button stats;

    public static boolean right(String[] a,String b)
    //in=String array and string
    //out=if the string is equal/close to one of the strings in the array-true else-false
    {
        boolean flag=false;
        for(int i=0;i<a.length;i++)
        {   
            int count=0;
            if(a[i].length()==b.length()){
                if(a[i].equalsIgnoreCase(b))
                    return true;
                else{if(a[i].length()>6){
                    for(int j=0;j<a[i].length();j++)
                    {
                        if(a[i].charAt(j)==b.charAt(j))
                            count++;
                    }
                    if(count==a[i].length()-1)
                        flag=true;
                    }
                }
            }
        }
        return flag;
    }

    public void messageBox(String[] a,String b,String answer,TextView tv,int points)
    //in=get the answer of the "right" method and the present name
    //out=if true-change the "TVajax" to the present name and pop up a message; false-nothing
    {   
        boolean f=false;
        if(right(a,b))
        {
            tv.setText(answer);
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("points: "+points);
            builder.setTitle("Perfect!");
            AlertDialog dialog = builder.create();
            builder.setPositiveButton(R.id.bStats, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Intent stats=new Intent(Ajax.this,Stats.class);
                    startActivity(stats);
                }
            });
            builder.setNegativeButton(R.id.bBack, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
        }
        else{
            if(b.length()<=6){
                for(int i=0;i<a.length&&!f;i++)
                {   
                    int fl=-1;
                    int count=0;
                    int diff=a[i].length()-b.length();
                    switch(diff){
                    case 0:
                        for(int j=0;j<a[i].length();j++)
                        {
                            if(a[i].charAt(j)==b.charAt(j))
                                count++;
                            if(count==a[i].length()-1){
                                f=true;
                                CharSequence text = "Almost perfect,you'r close!";
                                int duration = Toast.LENGTH_SHORT;
                                Toast popuptoast=Toast.makeText(context, text, duration);
                                popuptoast.show();
                            }
                        }
                        break;
                    case 1:
                        if(a[i].startsWith(b)||a[i].endsWith(b)){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }
                        break;
                    case -1:
                        if(b.startsWith(a[i])||b.endsWith(a[i])){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }
                        else{

                        for(int j=1;j<a[i].length()&&fl==-1;j++)
                        {   
                            if(a[i].charAt(j)!=b.charAt(j)&&a[i].charAt(j)==b.charAt(j+1)){
                                fl=j;
                                if(a[i].substring(0, j).equalsIgnoreCase(b.substring(0, j))&&a[i].substring(j).equalsIgnoreCase(b.substring(j+1))){
                                    f=true;
                                    CharSequence text = "Almost perfect,you'r close!";
                                    int duration = Toast.LENGTH_SHORT;
                                    Toast popuptoast=Toast.makeText(context, text, duration);
                                    popuptoast.show();
                                }
                            }


                        }
                    }
                    default:;
                    }
                }

            }else{
                for(int i=0;i<a.length&&!f;i++)
                {   
                    int fm=-1,sm=-1;
                    int diff=a[i].length()-b.length();
                    int count=0;
                    switch(diff){
                    case 0:
                        for(int j=0;j<a[i].length();j++)
                        {
                            if(a[i].charAt(j)==b.charAt(j))
                                count++;
                            if(count==a[i].length()-2){
                                f=true;
                                CharSequence text = "Almost perfect,you'r close!";
                                int duration = Toast.LENGTH_SHORT;
                                Toast popuptoast=Toast.makeText(context, text, duration);
                                popuptoast.show();
                            }
                        }
                        break;
                    case 1:
                        if(b.startsWith(a[i])||b.endsWith(a[i])){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }                           
                        break;
                    case 2:
                        if(a[i].indexOf(b)!=-1){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }
                        break;
                    case -1:
                        for(int j=0;j<a[i].length()&&(fm==-1||sm==-1);j++)
                        {
                            if(a[i].charAt(j)!=b.charAt(j)&&fm==-1)
                                fm=j;
                            if(a[i].charAt(j)!=b.charAt(j)&&fm!=-1)
                                sm=j;
                        }
                        if(b.substring(0,fm).equalsIgnoreCase(a[i].substring(0, fm))&&b.substring(fm+1, sm).equalsIgnoreCase(a[i].substring(fm, sm))&&b.substring(sm+1).equalsIgnoreCase(a[i].substring(sm))){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }
                        break;
                    case -2:
                        if(b.indexOf(a[i])!=-1){
                            f=true;
                            CharSequence text = "Almost perfect,you'r close!";
                            int duration = Toast.LENGTH_SHORT;
                            Toast popuptoast=Toast.makeText(context, text, duration);
                            popuptoast.show();
                        }
                        break;
                    }
                }
            }
            if(!f){
            CharSequence text = "wrong answer,try again!";
            int duration = Toast.LENGTH_SHORT;
            Toast popuptoast=Toast.makeText(context, text, duration);
            popuptoast.show();
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajax);
        initialize();
        check.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String[] answers={"amsterdamsche football club ajax nv","ajax","afc","afc ajax","afc ajax amsterdam","amsterdam","ajax amsterdam","ajax afc","ajax amsterdam afc","afc amsterdam","amsterdam ajax afc","amsterdam ajax"};
                answer = (EditText) findViewById(R.id.ETajax);
                String answera=answer.getText().toString();

            }
        });
    }

    private void initialize() {
        // TODO Auto-generated method stub
        TVajax= (TextView) findViewById(R.id.TVajax);
        hint1 = (Button) findViewById(R.id.Hint1);
        hint2 = (Button) findViewById(R.id.Hint2);
        check = (Button) findViewById(R.id.Bcheck);
        right = (Button) findViewById(R.id.BshowLetters);
        used = (Button) findViewById(R.id.BusedLetters);
                stats = (Button) findViewById(R.id.bStats);
    }

}

logs:

06-26 22:22:46.875: E/AndroidRuntime(683): FATAL EXCEPTION: main
06-26 22:22:46.875: E/AndroidRuntime(683): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.Ajax}: java.lang.NullPointerException
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.os.Looper.loop(Looper.java:123)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 22:22:46.875: E/AndroidRuntime(683):  at java.lang.reflect.Method.invokeNative(Native Method)
06-26 22:22:46.875: E/AndroidRuntime(683):  at java.lang.reflect.Method.invoke(Method.java:507)
06-26 22:22:46.875: E/AndroidRuntime(683):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 22:22:46.875: E/AndroidRuntime(683):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 22:22:46.875: E/AndroidRuntime(683):  at dalvik.system.NativeStart.main(Native Method)
06-26 22:22:46.875: E/AndroidRuntime(683): Caused by: java.lang.NullPointerException
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
06-26 22:22:46.875: E/AndroidRuntime(683):  at com.app.Ajax.<init>(Ajax.java:18)
06-26 22:22:46.875: E/AndroidRuntime(683):  at java.lang.Class.newInstanceImpl(Native Method)
06-26 22:22:46.875: E/AndroidRuntime(683):  at java.lang.Class.newInstance(Class.java:1409)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-26 22:22:46.875: E/AndroidRuntime(683):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-26 22:22:46.875: E/AndroidRuntime(683):  ... 11 more
4

2 回答 2

0

You have call your Intent

ajax

And your imagebutton as the same name.

Change the name of your Intent and it might be good.

于 2013-06-27T08:39:20.573 回答
0

You cannot initialize button like this Button stats=(Button) findViewById(R.id.bStats); This tries to initialize the fields but when activity is instantiated, but it still does not have the contentView

Initialize your button in your initialize method like other objects.

于 2013-06-27T17:32:40.270 回答