很抱歉只是转储代码,但我正在尝试制作菜单。当你启动程序时,它只是说“不幸的是,ScoutingDraft已停止工作了”。我无法弄清楚问题。
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;
}
}
我还没有任何其他java文件(这是到目前为止的整个程序)但是我给它的唯一选项应该触发3个if语句中的一个并在尝试创建类之前返回。即使这不是真的,它也不会崩溃,直到我点击某些东西。
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}
答案 0 :(得分:0)
看起来你得到null pointer exception
这是由你在第18行的一个观点引起的。
这是造成问题的原因:
setListAdapter(new ArrayAdapter<String>(MyMenu.this,android.R.layout.simple_list_item_1,display));
尝试:
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,display));
答案 1 :(得分:0)
事实证明我需要删除
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);
从顶部,将它们全部更改为最终变量,然后在if语句中移动所有内容:
if (classes[position].equals("login()")){