Reputation: 189
I'm a noob, and i need help developing a simple quiz app with if, else statement. I know i've got some problems with my code, but i don't know how to solve them. Even if it doesn't display any error, the app crashes when i try to open the activity called "FirstImageLogo". An ImageButton should open it. Inside "FirstImageLogo" i put a TextView and a Button, where the user should write the correct answer. If he does, than the button should open a new activity; if he doesn't, than the button should open a Toast message. The message that the user should write down inside TextView in this case is "Facebook". Unfortunately i have NO programming knowledge :( Anyway, here's my "FirstImageLogo.class" code:
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);
}
}
And here is the log file:
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
Thank you so much for your time.
Upvotes: 1
Views: 683
Reputation: 9326
you never initialized btnNext
. use the following
btnNext=(Button) findViewById(R.id.id_of_button);
then use setOnClickListener
Upvotes: 1
Reputation: 44571
The problem is here
btnNext.setOnClickListener(new View.OnClickListener()
btnNext
is null
because you haven't initialized it.
setContentView(R.layout.activity_first_image_logo);
inputtxt = (EditText) findViewById(R.id.text1);
btnNext = (Button) findViewById(R.id.idOfButton); // you need this
btnNext.setOnClickListener(new View.OnClickListener()
Upvotes: 1
Reputation: 93842
You never instantiate your btnNext
object.
Hence when doing btnNext.setOnClickListener
, it throws a NullPointerException
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_image_logo);
inputtxt = (EditText) findViewById(R.id.text1);
btnNext = (Button) findViewById(R.id.btnNext);//add this line
Upvotes: 1