Android - 调用对象时崩溃

时间:2013-03-06 15:26:38

标签: java android function methods

我现在有一个工作代码与我的论文但我决定使用函数/方法/对象清理它(不确定要调用它们)但是在组织它们之后,我的应用程序每次启动时都会崩溃。我真的不知道问题是什么。

主屏幕显示开始和退出按钮。当我按下START时,应用程序会说“遗憾的是论文停止了”。

我的代码是这样的:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.items, menu);
    View v = (View) menu.findItem(R.id.search).getActionView();
    final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
    txtSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            String curtextArray = txtSearch.getText().toString();
            char[] curletters = curtextArray.toCharArray();
            char[] curenhancedLetters = curtextArray.toCharArray();

//probably, problem here on Stemming stem
            Stemming stem = new Stemming(curtextArray, curletters, curenhancedLetters);
            AsyncTaskRunner newTask = new AsyncTaskRunner(enhancedStem);

// and probably, problem is here on the stem.<x process>;
            stem.removeApostrophe();
            stem.vowelMarking();
            stem.shortSyllable();
            if (continueStem == 1){
                stem.region1();
                stem.region2();
                stem.shortWord();
                stem.step0();
                stem.step1a();
                stem.step1b();
                newTask.execute();
            }
            return false;
        };
    });     
    return super.onCreateOptionsMenu(menu);

}

这是我的词干分类

public class Stemming {
    String textArray;
    char[] letters;
    char[] enhancedLetters;


    public Stemming (String curtextArray, char[] curletters, char[] curenhancedLetters){
        this.textArray = curtextArray;
        this.letters = curletters;
        this.enhancedLetters = curenhancedLetters;
    }

    public Stemming(){

    }

     public void removeApostrophe(){
       ...processes here
     }
     public void vowelMarking(){
       ...processes here
     }
     public void shortSyllable(){
       ...processes here
     }

    public void region1(){
       ...processes here
     }
     public void region2(){
       ...processes here
     }
     public void shortWord(){
       ...processes here
     }
     public void step0(){
       ...processes here
     }
     public void step1a(){
       ...processes here
     }
     public void step1b(){
       ...processes here
     }
}

}

我有一个关于崩溃原因的理论。这种方法可行吗? (伪代码):

public class Stemming {
String result;
String sample = "A A A A A";

public void changeAtoB{

    //do process to convert all As to Bs making String sample = "B B B B B"
    result = sample;
}

   public void changeBtoC{
   //do process to convert all Bs to Cs making String result = "C C C C C"
   result = result;
}

   ... so on {
   }
}

我所做的是直接处理字符串而不进行任何变量声明(我的变量全局声明)或初始化。我也没有发表任何回复声明。

我的代码曾经在没有那些函数/方法/对象时工作。

对我的长篇帖子抱歉。不知道如何更好地解释它。我希望你帮助我。提前谢谢!

logcat的:

>E/AndroidRuntime(11007): FATAL EXCEPTION: main
E/AndroidRuntime(11007): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.atienzaerni.thesis/com.atienzaerni.thesis.secondactivity}: java.lang.NullPointerException
E/AndroidRuntime(11007): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
E/AndroidRuntime(11007): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
E/AndroidRuntime(11007): at android.app.ActivityThread.access$600(ActivityThread.java:127)
E/AndroidRuntime(11007): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
E/AndroidRuntime(11007): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(11007): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(11007): at android.app.ActivityThread.main(ActivityThread.java:4441)
E/AndroidRuntime(11007): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11007): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(11007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(11007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(11007): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(11007): Caused by: java.lang.NullPointerException
E/AndroidRuntime(11007): at android.app.Activity.findViewById(Activity.java:1794)
E/AndroidRuntime(11007): at com.atienzaerni.thesis.secondactivity.<init>(secondactivity.java:54)
E/AndroidRuntime(11007): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(11007): at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(11007): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
E/AndroidRuntime(11007): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
E/AndroidRuntime(11007): ... 11 more
I/Process(11007): Sending signal. PID: 11007 SIG: 9

(我正在使用我的手机。不是模拟器。如果这有所不同)

1 个答案:

答案 0 :(得分:0)

看起来你得到一个NullPointerException,因为此行的v为null:

    final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);

这可能是由菜单项操作视图返回null引起的:

    View v = (View) menu.findItem(R.id.search).getActionView();

您应该在代码中设置操作视图,而不是调用getActionView,或者确保在菜单XML中设置了正确的操作视图。如果没有看到您的菜单XML,很难判断问题是否存在于XML中。