简体   繁体   中英

How do I make it wait until a function has returned? Android

Sorry to just dump code, but I'm trying to make a menu. When you start the program, It just says "Unfortunately, ScoutingDraft had stopped working." I can't figure out the problem.

package cody.graham.scoutingdraft;

import...;

public class MyMenu extends ListActivity{
    String UserName="a";
    String display[] = {"Welcome" ,"Login"  ,"Exit"    };
    String classes[] = {"return()","login()","finish()"};
    EditText user=(EditText) findViewById(R.id.ETUserInput);
    Button login=(Button) findViewById(R.id.BLogin),logout=(Button) findViewById(R.id.BLogout);
    TextView errmsg=(TextView) findViewById(R.id.TVErrorReport);
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(MyMenu.this,android.R.layout.simple_list_item_1,display));
    }
    @Override public void onBackPressed(){finish();}
    @Override
    protected void onListItemClick(ListView l, View v, int position,long id)
    {
        super.onListItemClick(l, v, position, id);
        if (classes[position].equals("return()")){return;}
        if (classes[position].equals("finish()")){finish();return;}
        if (classes[position].equals("login()")){
            final Dialog logd=new Dialog(this);
            logd.setContentView(R.layout.login_dialog);
            logd.setCancelable(false);
            login.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    int len=user.getText().toString().length();
                    if (len<3){errmsg.setText("Too short"); return;}
                    if (len>10){errmsg.setText("Too long"); return;}
                    UserName=user.getText().toString();
                    display[1]="Welcome, "+UserName;
                    display[2]="Logout";
                    logd.dismiss();
                }
            });
            logout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    UserName="a";
                    display[1]="Welcome";
                    display[2]="Login";
                    logd.dismiss();
                }
            });
            logd.show();
            return;
        }
        try{
            Class<?> cvar=Class.forName("cody.graham.scoutingdraft."+classes[position]);
            Intent intention=new Intent(MyMenu.this,cvar);
            startActivity(intention);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public String getUser(){
        return UserName;
    }
    public void setUser(String newuser){
        UserName=newuser;
    }
}

I don't have any other java files yet(This is the whole program so far) but the only options I give it should trigger one of the 3 if statements and return before trying to create the class. Even if this weren't true, it shouldn't crash until I click something.

LogCat:

11-12 17:24:45.925: I/ActivityManager(165): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cody.graham.scoutingdraft/.MyMenu bnds=[0,405][120,555] u=0} from pid 263
11-12 17:24:45.956: W/WindowManager(165): Failure taking screenshot for (246x410) to layer 21005
11-12 17:24:45.975: E/PowerManagerService(165): Excessive delay setting brightness: 140ms, mask=2
11-12 17:24:46.137: D/dalvikvm(6708): Not late-enabling CheckJNI (already on)
11-12 17:24:46.155: I/ActivityManager(165): Start proc cody.graham.scoutingdraft for activity cody.graham.scoutingdraft/.MyMenu: pid=6708 uid=10047 gids={1028}
11-12 17:24:46.235: E/PowerManagerService(165): Excessive delay setting brightness: 259ms, mask=2
11-12 17:24:46.525: E/PowerManagerService(165): Excessive delay setting brightness: 266ms, mask=2
11-12 17:24:46.606: I/Choreographer(165): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-12 17:24:46.708: E/PowerManagerService(165): Excessive delay setting brightness: 176ms, mask=2
11-12 17:24:47.005: E/PowerManagerService(165): Excessive delay setting brightness: 284ms, mask=2
11-12 17:24:47.146: E/Trace(6708): error opening trace file: No such file or directory (2)
11-12 17:24:47.656: D/AndroidRuntime(6708): Shutting down VM
11-12 17:24:47.656: W/dalvikvm(6708): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-12 17:24:47.686: E/AndroidRuntime(6708): FATAL EXCEPTION: main
11-12 17:24:47.686: E/AndroidRuntime(6708): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cody.graham.scoutingdraft/cody.graham.scoutingdraft.MyMenu}: java.lang.NullPointerException
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.os.Looper.loop(Looper.java:137)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at dalvik.system.NativeStart.main(Native Method)
11-12 17:24:47.686: E/AndroidRuntime(6708): Caused by: java.lang.NullPointerException
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.Activity.findViewById(Activity.java:1825)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at cody.graham.scoutingdraft.MyMenu.<init>(MyMenu.java:18)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at java.lang.Class.newInstanceImpl(Native Method)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at java.lang.Class.newInstance(Class.java:1319)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
11-12 17:24:47.686: E/AndroidRuntime(6708):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
11-12 17:24:47.686: E/AndroidRuntime(6708):     ... 11 more
11-12 17:24:47.716: W/ActivityManager(165):   Force finishing activity cody.graham.scoutingdraft/.MyMenu
11-12 17:24:47.726: W/WindowManager(165): Failure taking screenshot for (246x410) to layer 21010
11-12 17:24:48.266: W/ActivityManager(165): Activity pause timeout for ActivityRecord{41980720 cody.graham.scoutingdraft/.MyMenu}

It looks like you are getting a null pointer exception this is being caused by one of your views at line 18.

This is the line that is causing your problem:

setListAdapter(new ArrayAdapter<String>(MyMenu.this,android.R.layout.simple_list_item_1,display));

Try:

setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,display));

It turns out I needed to remove

    EditText user=(EditText) findViewById(R.id.ETUserInput);
    Button login=(Button) findViewById(R.id.BLogin),logout=(Button) findViewById(R.id.BLogout);
    TextView errmsg=(TextView) findViewById(R.id.TVErrorReport);

from the top, change them all to final variables, and move then all within the if statement:

        if (classes[position].equals("login()")){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM