AoN
Internet Programmer
- Aug 1, 2012
- 114
Alright, so a few years ago, when Android was relatively new, I tried my hand at a few tutorials that I seldom got to work. *cry* One of them, https://www.youtube.com/watch?v=rm-hNlTD1H0, has always bugged me. Now that I'm in a mobile apps course, I thought I'd revisit it. My problem is a confusing one. I have absolutely no errors when I compile, but it keeps crashing. Here's the MainActivity.java, fragment_main.xml, and string.xml files I have.
My the logcat produces the following error.
My AVD is a Nexus One running 4.1.2 (API 16), HVGA skinned. Of course, I checked the code, trying to figure out the error, but I couldn't find the issue beyond it being the setText(). If I comment out line 40, then 41 errors, eliminating the problem being String.valueOf(). I even tried changing the datatype to EditText instead of TextView, hence why it's been imported, but had no change to the error.
I've rewatched the tutorial and couldn't find anything wrong with my code compared to theirs. Any ideas on where I went wrong?
Code:
package com.example.golfScoreTut;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private static final String TAG = "GolfScore";
Button buttonMinus, buttonPlus, buttonOk;
TextView textScore, textScoreCard;
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
buttonMinus = (Button) findViewById(R.id.buttonMinus);
buttonPlus = (Button) findViewById(R.id.buttonPlus);
buttonOk = (Button) findViewById(R.id.buttonOk);
textScore = (TextView) findViewById(R.id.textScore);
textScoreCard = (TextView) findViewById(R.id.textSccreCard);
// Initialize scores
textScore.setText(String.valueOf(score));
textScoreCard.setText("");
// Listen for button clicks
buttonMinus.setOnClickListener(this);
buttonPlus.setOnClickListener(this);
buttonOk.setOnClickListener(this);
Log.d(TAG, "onCreate done");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
@Override
public void onClick(View src) {
switch(src.getId()) {
case R.id.buttonMinus:
score--;
break;
case R.id.buttonPlus:
score++;
break;
case R.id.buttonOk:
textScoreCard.append("\nYou played " + score);
score = 0;
break;
}
textScore.setText(String.valueOf(score));
}
}
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="@string/title"
android:textSize="24sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-" />
<Button
android:id="@+id/buttonPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="@+id/textScore"
android:layout_width="60sp"
android:layout_height="match_parent"
android:gravity="center"
android:text="99"
android:textSize="24sp" />
<Button
android:id="@+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
</LinearLayout>
<TextView
android:id="@+id/textSccreCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="Score card info goes here"
android:textSize="24sp" />
</LinearLayout>
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Assignment 9</string>
<string name="title">Golf Score</string>
<string name="action_settings">Settings</string>
</resources>
Code:
04-27 16:24:41.140: W/dalvikvm(6184): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-27 16:24:41.149: E/AndroidRuntime(6184): FATAL EXCEPTION: main
04-27 16:24:41.149: E/AndroidRuntime(6184): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.golfScoreTut/com.example.golfScoreTut.MainActivity}: java.lang.NullPointerException
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.os.Looper.loop(Looper.java:137)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-27 16:24:41.149: E/AndroidRuntime(6184): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 16:24:41.149: E/AndroidRuntime(6184): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 16:24:41.149: E/AndroidRuntime(6184): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-27 16:24:41.149: E/AndroidRuntime(6184): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-27 16:24:41.149: E/AndroidRuntime(6184): at dalvik.system.NativeStart.main(Native Method)
04-27 16:24:41.149: E/AndroidRuntime(6184): Caused by: java.lang.NullPointerException
04-27 16:24:41.149: E/AndroidRuntime(6184): at com.example.golfScoreTut.MainActivity.onCreate(MainActivity.java:40)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.Activity.performCreate(Activity.java:5008)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-27 16:24:41.149: E/AndroidRuntime(6184): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-27 16:24:41.149: E/AndroidRuntime(6184): ... 11 more
I've rewatched the tutorial and couldn't find anything wrong with my code compared to theirs. Any ideas on where I went wrong?