android


Android App Closes on creation?


I am fairly new to android and the app shuts down immediately after creation. Any help will be greatly appreciated!
A little about the app, I am creating a text based adventure and I have all the code coded into java already, and I know the java code works because I have a JavaGui created and working for it. I am now trying to bring this java game to android.
Here is my error log
05-29 09:19:01.989: E/AndroidRuntime(539): FATAL EXCEPTION: main
05-29 09:19:01.989: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.coalbolttproductions.russell/com.coalbolttproductions.russell.AdventureToCandyIslandActivity}: java.lang.NullPointerException
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:137)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 09:19:01.989: E/AndroidRuntime(539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 09:19:01.989: E/AndroidRuntime(539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 09:19:01.989: E/AndroidRuntime(539): at dalvik.system.NativeStart.main(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.Activity.findViewById(Activity.java:1794)
05-29 09:19:01.989: E/AndroidRuntime(539): at com.coalbolttproductions.russell.AdventureToCandyIslandActivity.<init>(AdventureToCandyIslandActivity.java:15)
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.Class.newInstanceImpl(Native Method)
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.Class.newInstance(Class.java:1319)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
05-29 09:19:01.989: E/AndroidRuntime(539): ... 11 more
Here is my main.xml that I believe is the true cause of my problem
<?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"
android:padding="25dp">
<EditText
android:id="#+id/etCommands"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type a Command"
android:password="true" />
<Button
android:id="#+id/bResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Try Command" />
<TextView
android:id="#+id/tvResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView" />
</LinearLayout>
Due to popular demand here is the java code from my Adventure class. Line 15 is the Button chkCmd
public class AdventureToCandyIslandActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
Button chkCmd = (Button) findViewById(R.id.bResults);
EditText input;
TextView display;
Game gameModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Run the methods
createVars();
chkCmd.setOnClickListener(this);
}
private void createVars(){
gameModel = new Game();
input = (EditText) findViewById(R.id.etCommands);
display = (TextView) findViewById(R.id.tvResults);
display.setText(gameModel.printWelcome());
display.setGravity(Gravity.LEFT);
display.setTextColor(Color.WHITE);
}
public void onClick(View v) {
String inputLine = input.getText().toString();
Command command = Command.getCommand(inputLine);
String result = gameModel.processCommand(command);
if(result.equals("EXIT")) {
System.exit(0);
} else {
display.append("--------------------------------\n" + result + "\n");
}
}
}
Whatever view you are setting up is returning null have you double checked the ID is correct and that setContentView(R.layout.xxx) is pointing at the correct file?
Also if you have different layouts defined for portrait and landscape this can cause similar issues if the IDs of your views don't match.AdventureToCandyIslandActivity.java see line no 15
EDIT
Button chkCmd = (Button) findViewById(R.id.bResults);
You've initialised the button in the class loader, if you initialise it in the same place as the other views it will be fine.
CHANGE
Button chkCmd = (Button) findViewById(R.id.bResults);
EditText input;
TextView display;
Game gameModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TO THIS
Button chkCmd;
EditText input;
TextView display;
Game gameModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkCmd = (Button) findViewById(R.id.bResults);
AS before SETTING THE LAYOUT, You can not initialize the button which is a part of that layout.
Button chkCmd = (Button) findViewById(R.id.bResults);
You have used findViewById(R.id.bResults) before setting ContentView so use following code after removing it:
setContentView(R.layout.main);
chkCmd = (Button) findViewById(R.id.bResults);

Related Links

Android cursor method gives error when i close the database
Android main_activity simple Games pattern e.g. for Cardgame
converting ByteArray File from server to a Image in imageView
“--core-library” error integrating HttpUnit with android
Sort order not working with listView and cursor loader
Restore state of WebView
Google Cloud Messaging for android application, different environments for the same application
Does view count of a youtube video increase when I use Youtube player API to play the video in my android app?
Installing AOSP on nexus 7 (Wifi)
Android app performance meter automation
Reaching the bandwidth limit for viewing pdf files in WebView through Google docs
Integrat Ecwid shopping cart with my android application
Android: Querying a mail server
Retrieving json data in Custom ListView?
onActivityResult RESULT_CODE always 0 (cancel)
Connect from Android Browser to Virtual Machine IP

Categories

HOME
notepad++
adal
jpa
fpga
nunit
case
gradient
babeljs
google-admin-sdk
google-schemas
mod-security
supervisor
endpoint
package.json
feathersjs
production
sigma.js
nim
pimcore
mount
pic32
outliers
8051
wacom
hard-drive
mnist
linq-to-objects
urlsession
dbnull
show-hide
httr
fwrite
openresty
payara-micro
edit
ngmaterial
ziparchive
segment-io
mta
rspec2
turbo-prolog
ikiwiki
fink
thread-sleep
pushbots
cycle2
dynatree
fiware-monitoring
flock
poppler
cgi-bin
rpart
fail2ban
j-security-check
shopizer
umlgraph
phpldapadmin
delayed-execution
entry-point
jqmodal
build-script
rcaller
renderer
spring-3
mongohq
cffile
android-icons
bjyauthorize
midlet
veracity
paginator
wiimote
infobright
expression-blend-4
cac
commerceserver2007
object-database
message-passing
folding
request-headers
wysiwym
int64
blind
cardspace

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App