0

I am trying to accept a phrase and pass it to other class called Animation.And I am using a self defined class called Error to store if error happens and what word causes the error.But when the button representing the class is clicked it displays "unfortunately the application has stopped".Check out partial code of the class below. Thank you for your help in advance.

 public class Convert extends Activity implements OnClickListener  {
 EditText inputConvert;
 TextView correct;
 String correction;
 String[] message = getResources().getStringArray(R.array.basic_words);
 final Context context = this; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.convert);
           View convert = findViewById(R.id.conv_button);
           convert.setOnClickListener(this);

        }

    public void onClick(View v) 
    {

        inputConvert=(EditText) findViewById(R.id.inputConvert);
        String  phrase = inputConvert.getText().toString();

              if(v.getId()==R.id.conv_button)
              {

                  Error obj1= new Error(false ,"initial");
                  obj1=check(phrase);

                  if(obj1.flag)  
                      startAnim(phrase);
                  else
                  {
                       correction = build(obj1.word);
                     final Dialog dialog = new Dialog(context);
                       dialog.setContentView(R.layout.error);
 Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
correct= (TextView) findViewById(R.id.error_content);
        correct.setText(correction);
        dialog.setTitle(R.string.help);
     dialogButton.setOnClickListener(new OnClickListener() {
                            @Override
                        public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                  }
               }
    }

   public String build(String word)
             {
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.append("the word \"");
     stringBuilder.append(word);
     stringBuilder.append(" \" is not found please try to use a synomym");
             String finalString = stringBuilder.toString();
             return finalString;
             }  



  public Error check(String word)
   {
      Error obj2 = new Error(false ,"initial");
      obj2.word=word;
      String[] words = word.split(" ");

              for(int i=0; i< words.length; i++)
                {
                  for(int j=0; i< message.length; j++)
                  {
                     if(words[i]==message[j])
                        obj2.flag=true;
                        break;
                  }
                     if(!obj2.flag)
                         obj2.word=words[i];
                         break;
                }
         return obj2;

         }


    public void startAnim(String phrase)
        {
            Intent j = new Intent(this, Animation.class);
            j.putExtra("phrase",phrase);
            startActivity(j);   
        }

 Here is the code of the Animation activity.
public class Animation extends Activity
{
 AnimationDrawable animation;
 TextView errorMessage= (TextView)findViewById(R.id.testex);

 @SuppressWarnings("deprecation")

public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation);
        Intent j= getIntent(); 
        CharSequence  message = j.getStringExtra("phrase");
        String phrase=message.toString();
        String[] words = phrase.split(" ");



        animation = new AnimationDrawable();
        int imid=0;
        for(int i=0; i< words.length; i++)
        {
       imid=getResources().getIdentifier(words[i], "drawable", getPackageName());
             animation.addFrame(getResources().getDrawable(imid), 700);
        }

        animation.setOneShot(false);
        ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
        img.setBackgroundDrawable(animation);
        animation = (AnimationDrawable) img.getBackground();

         // Start the animation (looped playback by default).
           animation.start();
 }     
   }

 Logcat.

12-12 08:32:04.962: E/AndroidRuntime(2273): FATAL EXCEPTION: main 12-12 08:32:04.962: E/AndroidRuntime(2273): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.ihear/org.example.ihear.Convert}: java.lang.NullPointerException 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.access$600(ActivityThread.java:141) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Handler.dispatchMessage(Handler.java:99) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.os.Looper.loop(Looper.java:137) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.main(ActivityThread.java:5103) 12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invokeNative(Native Method) 12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.reflect.Method.invoke(Method.java:525) 12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 12-12 08:32:04.962: E/AndroidRuntime(2273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-12 08:32:04.962: E/AndroidRuntime(2273): at dalvik.system.NativeStart.main(Native Method) 12-12 08:32:04.962: E/AndroidRuntime(2273): Caused by: java.lang.NullPointerException 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 12-12 08:32:04.962: E/AndroidRuntime(2273): at org.example.ihear.Convert.(Convert.java:19) 12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstanceImpl(Native Method) 12-12 08:32:04.962: E/AndroidRuntime(2273): at java.lang.Class.newInstance(Class.java:1130) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 12-12 08:32:04.962: E/AndroidRuntime(2273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 12-12 08:32:04.962: E/AndroidRuntime(2273): ... 11 more

afom
  • 61
  • 1
  • 9

3 Answers3

0

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo:{....} means that Android OS could not create your activity.

The most probable error is that you forgot to add it to your AndroidManifest. I suspect that it is related to the Animation activity.

Try to declare

<activity android:name="**YOUR.PACKAGE**.Animation" />

See : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

If the declaration of the activity in the manifest was not the problem, try to inspect the constructor / onCreate methods of your Animation activity.

Community
  • 1
  • 1
kamidude
  • 1,301
  • 8
  • 13
  • I have added the Code of the Animation activity. Thank you for your help in advance. – afom Dec 13 '13 at 05:30
0

R.layout.convert is this layout right?

You can check it and make sure you have not used any element can not be found

MengMeng
  • 1,016
  • 6
  • 11
0

Caused by: java.lang.NullPointerException

at org.example.ihear.Convert.(Convert.java:19)

here is your real error.please check it.

venciallee
  • 775
  • 4
  • 19