gpt4 book ai didi

java - Android:带有 if else 语句的测验应用程序

转载 作者:行者123 更新时间:2023-11-29 05:30:39 24 4
gpt4 key购买 nike

我是一个菜鸟,我需要帮助开发一个简单的测验应用程序,其中包含 if, else 语句。我知道我的代码有一些问题,但我不知道如何解决。即使它没有显示任何错误,当我尝试打开名为“FirstImageLogo”的 Activity 时,应用程序也会崩溃。ImageButton 应该打开它。在“FirstImageLogo”中我放了一个 TextView 和一个 Button,用户应该在其中写下正确的答案。如果他这样做,那么按钮应该打开一个新的 Activity ;如果他不这样做,则该按钮应打开一条 Toast 消息。在这种情况下,用户应该在 TextView 中写下的消息是“Facebook”。不幸的是我没有编程知识:(不管怎样,这是我的“FirstImageLogo.class”代码:

private EditText inputtxt;
private Button btnNext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_image_logo);

inputtxt = (EditText) findViewById(R.id.text1);

btnNext.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;

name=inputtxt.getText().toString();

if (name.equalsIgnoreCase("facebook"))
{

Intent intent = new Intent (FirstImageLogo.this, GoodJob.class);
startActivity(intent);

}
else
{ Toast.makeText(getApplicationContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
}

});

setupActionBar();
}


/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first_image_logo, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

这是日志文件:

01-17 21:35:39.365: D/dalvikvm(19074): GC_FOR_ALLOC freed 68K, 1% free 17135K/17236K, paused 14ms, total 14ms
01-17 21:35:39.905: D/dalvikvm(19074): GC_FOR_ALLOC freed 21K, 1% free 17547K/17604K, paused 7ms, total 7ms
01-17 21:35:39.915: D/AndroidRuntime(19074): Shutting down VM
01-17 21:35:39.915: W/dalvikvm(19074): threadid=1: thread exiting with uncaught exception (group=0x41596ba8)
01-17 21:35:39.915: E/AndroidRuntime(19074): FATAL EXCEPTION: main
01-17 21:35:39.915: E/AndroidRuntime(19074): Process: com.example.appquiz, PID: 19074
01-17 21:35:39.915: E/AndroidRuntime(19074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appquiz/com.example.appquiz.FirstImageLogo}: java.lang.NullPointerException
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.os.Looper.loop(Looper.java:136)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-17 21:35:39.915: E/AndroidRuntime(19074): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 21:35:39.915: E/AndroidRuntime(19074): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-17 21:35:39.915: E/AndroidRuntime(19074): at dalvik.system.NativeStart.main(Native Method)
01-17 21:35:39.915: E/AndroidRuntime(19074): Caused by: java.lang.NullPointerException
01-17 21:35:39.915: E/AndroidRuntime(19074): at com.example.appquiz.FirstImageLogo.onCreate(FirstImageLogo.java:28)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.Activity.performCreate(Activity.java:5231)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-17 21:35:39.915: E/AndroidRuntime(19074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-17 21:35:39.915: E/AndroidRuntime(19074): ... 11 more

非常感谢您的宝贵时间。

最佳答案

您从未初始化btnNext。使用以下内容

btnNext=(Button) findViewById(R.id.id_of_button);

然后使用setOnClickListener

关于java - Android:带有 if else 语句的测验应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21195433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com