Laurenswuyts
Laurenswuyts

Reputation: 2142

java.lang.RuntimeException: Unable to start activity

One of the people who downloaded my app sended me a crash through google play. I found it withtin crashes and anr. but I have no idea what's wrong with it. With me it always works so I don't know what the problem is here. This is the logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laurenswuyts.find.it/com.laurenswuyts.find.it.MainKeyboard}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:154)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.laurenswuyts.find.it.MainKeyboard.onCreate(MainKeyboard.java:102)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more

And this is my code:

package com.laurenswuyts.find.it;

import java.util.Locale;

import com.laurenswuyts.find.it.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainKeyboard extends Activity {
public String[] taal;
String[] StringDA = Languages.StringDA;
String[] StringDE = Languages.StringDE;
String[] StringFR = Languages2.StringFR;
String[] StringNL = Languages.StringNL;
String[] StringEN = Languages.StringEN;
String[] StringPT = Languages2.StringPT;
String[] StringSV = Languages.StringSV;
String[] StringES = Languages2.StringES;
String[] StringNO = Languages2.StringNO;

public ImageButton Search;

public AutoCompleteTextView AutoCompleteTextView;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.mainkeyboard);





       Search =  (ImageButton) findViewById(R.id.imageButtonSearch);         

       final AutoCompleteTextView AutoCompleteTextView = (AutoCompleteTextView)
                findViewById(R.id.autoCompleteTextView1);
       if (Locale.getDefault().getLanguage().equals("da")) {
           taal = StringDA;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("de")) {
           taal = StringDE;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("fr")) {
           taal = StringFR;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("nl")) {
           taal = StringNL;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("no")) {
           taal = StringNO;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("pt")) {
           taal = StringPT;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("es")) {
           taal = StringES;
       } 
       else{}
       if (Locale.getDefault().getLanguage().equals("sv")) {
           taal = StringSV;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("en")) {
           taal = StringEN;
       }
       else{

       }

       Search.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(MainKeyboard.this,MainActivity.class);
               intent.putExtra("thetext",AutoCompleteTextView.getText().toString());
               startActivity(intent);
              }
          });

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.suggest, taal);

       AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
       acTextView.setThreshold(1);
       acTextView.setAdapter(adapter);
       acTextView.setOnItemClickListener(new OnItemClickListener() {
           public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
               Intent intent = new Intent(MainKeyboard.this,MainActivity.class);
               intent.putExtra("thetext",AutoCompleteTextView.getText().toString());
               startActivity(intent);
           }
       });
                AutoCompleteTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                                Intent intent = new Intent(MainKeyboard.this,MainActivity.class);
                                intent.putExtra("thetext",AutoCompleteTextView.getText().toString());
                                startActivity(intent);
                                return true;
                            }
                            return false;
                        }
                    });

   }

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.twitter:     Uri uri = Uri.parse("http://twitter.com/finditwurens");
         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
         startActivity(intent);
         break;
      case R.id.facebook:     Uri uri2 = Uri.parse("http://www.facebook.com/AppFindIt");
         Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
         startActivity(intent2);
         break;
      case R.id.googleplus: Uri uri3 = Uri.parse("http://plus.google.com/105389501412708876212/posts");
         Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
         startActivity(intent3);
         break;
      case R.id.addgoogle: Uri uri4 = Uri.parse("http://www.google.com/places/");
         Intent intent4 = new Intent(Intent.ACTION_VIEW, uri4);
         startActivity(intent4);    
         break;
        }
        return true;
    }


            }

Upvotes: 0

Views: 410

Answers (5)

Ahmad
Ahmad

Reputation: 72533

None of your conditions is true, I would advise you to set a default language in your else statement:

if (Locale.getDefault().getLanguage().equals("da")) {
           taal = StringDA;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("de")) {
           taal = StringDE;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("fr")) {
           taal = StringFR;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("nl")) {
           taal = StringNL;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("no")) {
           taal = StringNO;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("pt")) {
           taal = StringPT;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("es")) {
           taal = StringES;
       } 
       else{}
       if (Locale.getDefault().getLanguage().equals("sv")) {
           taal = StringSV;
       }
       else{}
       if (Locale.getDefault().getLanguage().equals("en")) {
           taal = StringEN;
       }
       else{
           taal = StringEN; //or whatever default language
       }

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

- You have actually never initialized the Array, public String[] taal;

- Secondly i would ask you to use ArrayList<String> instead of Array, as Collections are much more flexible than Arrays.

Upvotes: 0

Julien May
Julien May

Reputation: 2051

replace the following code:

if (Locale.getDefault().getLanguage().equals("en")) {
    taal = StringEN;
}
else{
}

with this one:

if (taal == null) {
    taal = StringEN;
}

That way english is set as the default language if you do not support the users specific language.

Upvotes: 1

richardtz
richardtz

Reputation: 4993

The problem is that the lenguage of the device is not whitin the ones you are considering.

Try initializaing yout taal variable to a default language String [].

Upvotes: 0

kosa
kosa

Reputation: 66637

It seems your String[] taal; is null; (none of the if/else could be satisfied, print and see which language your device is in by logging Locale.getDefault().getLanguage(), that may give some clue why none of the if/else satisfied)

by the time control reach

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.suggest, taal);

Which resulting in java.lang.NullPointerException

Make sure taal is not null.

Upvotes: 2

Related Questions