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

Prevent WebView from displaying detailed error
How to capture sound while doing video capture with Android Studio?
How to get altitude from Google Maps
How to resize images to fit a fixed size inside a CardView in Android?
Can't connect Android to desktop server
Intent Camera Crashes on Nexus Devices
Simplest Method for Android App with rtmp Video
Android Bitmap usage Optimization
Android | Google Sign | Get IdToken correctly in silent
Integrate firebase notifications topics and firebase authentication
Nesting a ScrollView inside a TableRow
AndroidStudio order mixed up when using Intents to open Activities
Prevent Joda-Time to print negative numbers
Getting email from Facebook - FB android SDK
Pick an image and save it in custom folder
Android Insistent Notification Interrupted By SMS - How to make uninterruptable?

Categories

HOME
jmeter
pyqt
rsa
pascal
boto3
scrolltop
cumulocity
coin3d
dimensional-modeling
asp.net-mvc-viewmodel
nim
enterprise-miner
dataflow-diagram
imageresizer
windows-server-2003
motion-detection
pylons
bringtofront
google-earth
powershell-ise
mockjax
testbed
hibernate-validator
rselenium
hibernate-search
yowsup
modernizr
flatmap
apache-stanbol
ogc
stringtokenizer
levenshtein-distance
flask-login
opennms
spring-integration-sftp
reportservice2010
fclose
payara-micro
opengl-4
gpg-signature
javafx-css
cronexpression
chromium-os
pure-css
beamer
observablecollection
pre-build-event
dynatree
rate-limiting
graphics2d
botan
telepat
mmwormhole
phpldapadmin
ejb-2.x
appserver
universal-image-loader
codeplex
cancellation
terminate
twitter-bootstrap-rails
qscrollarea
gwt-openlayers
bin
embedded-fonts
isqlquery
oocss
bjyauthorize
android-sdk-2.3
frameworkelementfactory
fragmenttransaction
vfs
subproject
multiplatform
openfaces
django-piston
strtod
data-mapping
glui
lobo-cobra
google-wave
ifilter

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