java.lang.RuntimeException:无法启动活动

时间:2012-12-11 16:37:55

标签: java android android-activity

下载我的应用程序的人之一通过谷歌播放给我一个崩溃。我发现它有崩溃和anr。但我不知道它有什么问题。对我来说它总是有效,所以我不知道这里有什么问题。这是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

这是我的代码:

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


            }

5 个答案:

答案 0 :(得分:2)

您的String[] taal;似乎是null;(没有if / else可以满足,打印并通过记录Locale.getDefault().getLanguage()查看您的设备所在的语言,这可能会给出一些线索为什么没有if / else满意的)

到时间控制范围

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

导致java.lang.NullPointerException

确保taal 不为空

答案 1 :(得分:1)

替换以下代码:

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

这一个:

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

如果您不支持用户特定语言,那么将英语设置为默认语言。

答案 2 :(得分:1)

你的条件都没有,我建议你在else语句中设置默认语言:

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
       }

答案 3 :(得分:0)

问题在于设备的语言并不是您正在考虑的语言。

尝试将taal变量初始化为默认语言String []

答案 4 :(得分:0)

- 实际上从未初始化数组public String[] taal;

- 其次,我会要求您使用ArrayList<String>代替Array,因为CollectionsArrays灵活得多。