Every time I attempt to test this the app crashes when I hit the "Next" button (butHOURS button). I've tried redoing the XML's, looking over the code several times but I can't figure it out. Any help would be mighty appreciated. This is also the second class in a series of 5 and the first (main) class switches just fine to this class/xml layout.
package com.pewpew.studentadvisor;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class Main2 extends Activity
{
public Button butHOURS;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
RadioButton hours1 = (RadioButton) findViewById(R.id.hours1);
RadioButton hours2 = (RadioButton) findViewById(R.id.hours2);
RadioButton hours3 = (RadioButton) findViewById(R.id.hours3);
RadioButton hours4 = (RadioButton) findViewById(R.id.hours4);
//Calculation based on ENROLLED question
if (hours1.isChecked()){Main.calculation = Main.calculation + 1;}
else{ if (hours2.isChecked()) {Main.calculation = Main.calculation + 2; }
else{ if (hours3.isChecked()) {Main.calculation = Main.calculation + 3; }
else{ if (hours4.isChecked()) {Main.calculation = Main.calculation + 4; }
else{Main.calculation = 0.0; }}}}
//Get button to do button stuff like go to the next page
Button butHOURS = (Button) findViewById(R.id.butHOURS);
butHOURS.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i = new Intent(Main2.this, Main3.class);
startActivity(i);
}
});
}
}
Here is the crash log
04-13 19:43:54.820: D/AndroidRuntime(26281): Shutting down VM
04-13 19:43:54.820: W/dalvikvm(26281): threadid=1: thread exiting with uncaught exception (group=0x41548360)
04-13 19:43:54.830: E/AndroidRuntime(26281): FATAL EXCEPTION: main
04-13 19:43:54.830: E/AndroidRuntime(26281): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.pewpew.studentadvisor/com.pewpew.studentadvisor.Main3}; have you declared this activity in your AndroidManifest.xml?
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Activity.startActivityForResult(Activity.java:3417)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Activity.startActivityForResult(Activity.java:3378)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Activity.startActivity(Activity.java:3588)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.Activity.startActivity(Activity.java:3556)
04-13 19:43:54.830: E/AndroidRuntime(26281): at com.pewpew.studentadvisor.Main2$1.onClick(Main2.java:43)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.view.View.performClick(View.java:4192)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.view.View$PerformClick.run(View.java:17254)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.os.Handler.handleCallback(Handler.java:615)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.os.Looper.loop(Looper.java:137)
04-13 19:43:54.830: E/AndroidRuntime(26281): at android.app.ActivityThread.main(ActivityThread.java:4950)
04-13 19:43:54.830: E/AndroidRuntime(26281): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:43:54.830: E/AndroidRuntime(26281): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:43:54.830: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 19:43:54.830: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 19:43:54.830: E/AndroidRuntime(26281): at dalvik.system.NativeStart.main(Native Method)
For anyone to find this later, my mistake was I had a typo in my AndroidManifest. >_<
Just remove the attribute "public Button butHOURS;"
tks
You already create a variable:
public Button butHOURS;
Then, you should use this variable as follows:
butHOURS = (Button) findViewById(R.id.butHOURS);
You variable is useless in your code. However, the real problem is (as you can see in your LogCat):
have you declared this activity in your AndroidManifest.xml?
You should declare Main3 Activity inside your Manifest as follows:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.pewpew.studentadvisor.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.pewpew.studentadvisor.Main3"
android:label="#string/app_name" />
// other activities
</application>
Every Activity in your app needs to be declared in your Manifest file as above.
Problem is in your Manifest file. You should declare a new Activity like this:
<activity android:name=".Main3"></activity> under your
<application> ... </aplication>
Related
I have a problem launching my application in my emulator. When I run it, I get the message "Sorry MyApp has stopped unexpectedly"
This is what I get in my LogCat:
12-22 06:59:42.196: D/AndroidRuntime(1083): Shutting down VM
12-22 06:59:42.196: W/dalvikvm(1083): threadid=1: thread exiting with uncaught exception (group=0xb60294f0)
12-22 06:59:42.285: E/AndroidRuntime(1083): FATAL EXCEPTION: main
12-22 06:59:42.285: E/AndroidRuntime(1083): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld5/com.example.helloworld5.MainActivity}: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.os.Looper.loop(Looper.java:130)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.reflect.Method.invoke(Method.java:507)
12-22 06:59:42.285: E/AndroidRuntime(1083): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-22 06:59:42.285: E/AndroidRuntime(1083): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-22 06:59:42.285: E/AndroidRuntime(1083): at dalvik.system.NativeStart.main(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083): at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.Class.newInstanceImpl(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.Class.newInstance(Class.java:1409)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-22 06:59:42.285: E/AndroidRuntime(1083): ... 11 more
So I looked in my Manifest file and changed
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
to:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld5.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
However when I run it after the change I get No launch activity found!.
This is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld5"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Floor"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.helloworld5.FLOOR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
This is my MainActivity code:
MainActivity.java
package com.example.helloworld5;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
//String name;
DrawView drawView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText dest = (EditText)findViewById(R.id.y_editText2);
//final String roomName = dest.getText().toString();
final Button openFloor = (Button)findViewById(R.id.y_button1);
openFloor.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
drawView.setRoomName(dest.getText().toString());
startActivity(new Intent("com.example.helloworld5.FLOOR"));
}
});
}
#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);
}
}
DrawView.java:
public class DrawView extends View {
Paint paint = new Paint();
float ux, dx, rx,lx;
String roomName2 ;
public DrawView(Context context) {
super(context);
paint.setColor(Color.RED);
//roomName2 = drawView.getTag();
}
public void setRoomName(String name) {
this.roomName2 = name;
}
public String getRoomName(){
return roomName2;
}
public void setCoordinates(){
};
#Override
public void onDraw(Canvas canvas) {
String roomName = getRoomName();
if(roomName == "C154"){
ux =90;
dx = 250;
rx = 90;
lx = 400;
}else {
ux =76;
dx = 98;
rx = 140;
lx = 300;
}
canvas.drawLine(90, 250 , 90, 400, paint);
canvas.drawLine(20, 0, 0, 20, paint);
canvas.drawCircle(150, 400, 30, paint);
}
}
How can I fix this crash?
According to this
12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083): at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.Class.newInstanceImpl(Native Method)
12-22 06:59:42.285: E/AndroidRuntime(1083): at java.lang.Class.newInstance(Class.java:1409)
You are calling findViewById in the constructor of your activity. However, by then, the layout hasnt been inflated. Move the method call to onCreate:
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
dest = (EditText)findViewById(R.id.y_editText2);
//et cetera
}
EDIT:
Also, you're setting String roomName = dest.getText().toString(); on a null reference now. set the onClickListener like this:
openFloor.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
drawView.setRoomName(dest.getText().toString);
startActivity(new Intent("com.example.helloworld5.FLOOR"));
}
});
EDIT2: Your second NPE is because DrawView is not initialized, initialize it as well:
public void onCreate(Bundle b) {
super.onCreate(b);
//...your other stuff
this.drawView = (DrawView) findViewById(R.id.myDrawView);
}
You should move
EditText dest = (EditText)findViewById(R.id.y_editText2);
inside onCreate(...) after setContentView(...)
Issue is:
12-22 06:59:42.285: E/AndroidRuntime(1083): Caused by: java.lang.NullPointerException
12-22 06:59:42.285: E/AndroidRuntime(1083): at android.app.Activity.findViewById(Activity.java:1647)
12-22 06:59:42.285: E/AndroidRuntime(1083): at com.example.helloworld5.MainActivity.<init>(MainActivity.java:14)
Put
EditText dest = (EditText)findViewById(R.id.y_editText2);
after your setContentView
This will just resolve current crash.
PLUS
put your
String roomName = dest.getText().toString();
in button click call.
If you will keep it in global variale section, it will give you NullPointerException for dest. And if you put this line in onCreate just after findViewById, it will just give you roomName = "" as at that time view has just been initialized. User entered value won't be there.
Your activity should be like this.
public class MainActivity extends ActionBarActivity {
DrawView drawView;
String roomName;
EditText dest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dest = (EditText)findViewById(R.id.y_editText2);
final Button openFloor = (Button)findViewById(R.id.y_button1);
openFloor.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
roomName = dest.getText().toString();
drawView.setRoomName(roomName);
startActivity(new Intent("com.example.helloworld5.FLOOR"));
}
});
}
}
I just started learning Android programming and have run into trouble. I'm using the book "Android Programming The Big Nerd Ranch Guide". My IDE is Eclipse and Genymotion to emulate the app. Here's the logcat:
04-13 00:19:48.065: D/dalvikvm(1256): Late-enabling CheckJNI
04-13 00:19:48.781: D/AndroidRuntime(1256): Shutting down VM
04-13 00:19:48.785: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d8ab20)
04-13 00:19:48.785: E/AndroidRuntime(1256): FATAL EXCEPTION: main
04-13 00:19:48.785: E/AndroidRuntime(1256): Process: com.bignerdranch.android.geoquiz, PID: 1256
04-13 00:19:48.785: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.QuizActivity}: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.os.Looper.loop(Looper.java:136)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-13 00:19:48.785: E/AndroidRuntime(1256): at dalvik.system.NativeStart.main(Native Method)
04-13 00:19:48.785: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
04-13 00:19:48.785: E/AndroidRuntime(1256): at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:30)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Activity.performCreate(Activity.java:5231)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-13 00:19:48.785: E/AndroidRuntime(1256): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-13 00:19:48.785: E/AndroidRuntime(1256): ... 11 more
04-13 00:19:56.345: I/Process(1256): Sending signal. PID: 1256 SIG: 9
Here's the fragment_quiz.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="#string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/true_button" />
<Button
android:id="#+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/false_button" />
</LinearLayout>
</LinearLayout>
The QuizActivity.java file (most of the code imported by Eclipse):
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT)
.show();
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT)
.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, 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_quiz, container,
false);
return rootView;
}
}
}
What am I doing wrong?
Put your layout definitions in the PlaceholderFragment class. For the findViewById, use rootView.findViewById. For the context, use getActivity().
Why does this work? There are actually 2 independent layouts shown as one in your screen. The master one shows everything, including a fragment. A Fragment is basically a self-contained mini activity, very useful in Tablet development, and mildly useful even just for a phone type device. Your layouts are in the fragment_quiz xml file, which is only inflated inside the fragment, in the onCreateView statement. So long as you use the findViewById after it's been inflated, you'll be fine.
this code works
public class listActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
setListAdapter(dataSource);
mDbHelper.close();
}
But when I try to set the adapter to a listview in one of my layout it just crashes
public class listActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
testAdapter mDbHelper = new testAdapter(this);
mDbHelper.open();
Cursor c = mDbHelper.getAllData();
String[] from = new String[] { "name", "title" };
CursorAdapter dataSource = new SimpleCursorAdapter(this,
R.layout.list_row, c, from, new int[] { R.id.name,
R.id.title});
ListView itemList = (ListView)findViewById(R.id.itemsList);
itemList.setAdapter(dataSource); // line 29 where it crashes
mDbHelper.close();
}
and this is what LogCat throws out
04-13 20:58:09.331: E/DataAdapter(929): opened db
04-13 20:58:09.451: D/AndroidRuntime(929): Shutting down VM
04-13 20:58:09.451: W/dalvikvm(929): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-13 20:58:09.521: E/AndroidRuntime(929): FATAL EXCEPTION: main
04-13 20:58:09.521: E/AndroidRuntime(929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.listActivity}: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.os.Looper.loop(Looper.java:123)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): at java.lang.reflect.Method.invoke(Method.java:507)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 20:58:09.521: E/AndroidRuntime(929): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 20:58:09.521: E/AndroidRuntime(929): at dalvik.system.NativeStart.main(Native Method)
04-13 20:58:09.521: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
04-13 20:58:09.521: E/AndroidRuntime(929): at com.exmaple.app.listActivity.onCreate(listActivity.java:29)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-13 20:58:09.521: E/AndroidRuntime(929): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-13 20:58:09.521: E/AndroidRuntime(929): ... 11 more
I´ve been strugling with this for weeks, so please any help will be appreciated.
You haven't called setContentView(), the code can't find your R.id.itemsList view. itemsList is null.
I have been attempting to make my first android application (a simple temperature converter) in Eclipse, but when I click the button on my phone the app crashes. Here is the full java code
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
String number;
int number2;
int output;
boolean F;
public void onBtnClicked(View view){
EditText mEdit = (EditText)findViewById(R.id.editText1);
TextView myTextView = (TextView) findViewById(R.id.label);
number = mEdit.getText().toString();
number2 = Integer.parseInt(number);
if(F=true){
output=number2*9/5+32;
}
else{
output=number2-32*5/9;
}
myTextView.setText(output);
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio0:
if (checked)
F = true;
break;
case R.id.radio1:
if (checked)
F = false;
break;
}
}
}
LogCat when the button is clicked
04-13 20:19:50.423: E/AndroidRuntime(25200): FATAL EXCEPTION: main
04-13 20:19:50.423: E/AndroidRuntime(25200): java.lang.IllegalStateException: Could not execute method of the activity
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$1.onClick(View.java:3674)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View.performClick(View.java:4198)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$PerformClick.run(View.java:17158)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Handler.handleCallback(Handler.java:615)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.os.Looper.loop(Looper.java:137)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-13 20:19:50.423: E/AndroidRuntime(25200): at dalvik.system.NativeStart.main(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: java.lang.reflect.InvocationTargetException
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:19:50.423: E/AndroidRuntime(25200): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.view.View$1.onClick(View.java:3669)
04-13 20:19:50.423: E/AndroidRuntime(25200): ... 11 more
04-13 20:19:50.423: E/AndroidRuntime(25200): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x59
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.content.res.Resources.getText(Resources.java:242)
04-13 20:19:50.423: E/AndroidRuntime(25200): at android.widget.TextView.setText(TextView.java:3773)
04-13 20:19:50.423: E/AndroidRuntime(25200): at com.example.myfirstapp.MainActivity.onBtnClicked(MainActivity.java:43)
04-13 20:19:50.423: E/AndroidRuntime(25200): ... 14 more
04-13 20:19:50.453: E/android.os.Debug(718): !#Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
And lastly for the xml of the button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="onBtnClicked"
android:text="Calculate" />
I'm not sure how to go about fixing this, so hopefully someone can help.
Thanks.
Initialize your Buttons first then set onclicklistener to them
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize views here
EditText mEdit = (EditText) findViewById(R.id.editText1);
TextView myTextView = (TextView) findViewById(R.id.label);
Button yourButton = (Button) findViewByid(R.id.youridforbutton);
//set onclicklistener for your button
yourbutton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
number = mEdit.getText().toString();
number2 = Integer.parseInt(number);
if (F = true) {
output = number2 * 9 / 5 + 32;
} else {
output = number2 - 32 * 5 / 9;
}
myTextView.setText("" + output);
}
});
}
Similarly set the other button also
You need to cast output to String
myTextView.setText(String.valueOf(output));
setText method is overloaded and when You pass an integer to it it expects it to be an id of resource.
You can not set Integers to TextViews. You have to convert them into Strings.
myTextView.setText(String.valueOf(output));
You need to tell the click listener to listen to a particular button. So, in other words set an OnItemClickListener on the button. Something as follows:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do the "on click" action here
}
});
Hope this helps. If not, then please comment with further issue.
I have created new activity that my MainActivity Should lunch, some why the application is crashing on the start of the new activity (called GamePlayActivity).
Here is the java code:
Intent startGameDrill = new Intent(MainActivity.this, GamePlayActivity.class);
startActivity(startGameDrill);
Here is the startGameDrill:
package com.simplemathgame;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GamePlayActivity extends MainActivity {
int addDrills;
int subDrils;
int mulDrills;
int divDrills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_play);
try {
numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
minBoundText = (TextView) findViewById(R.id.min_text);
maxBoundText = (TextView) findViewById(R.id.max_text);
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.w("game","error");
}
try {
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
subDrils = Integer.parseInt((String) numberOfSubDrills.getText());
mulDrills = Integer.parseInt((String) numberOfMulDrills.getText());
divDrills = Integer.parseInt((String) numberOfDivDrills.getText());
} catch (NumberFormatException e) {
Log.w("GameDrills","string to int");
}
Log.w("add", "" + addDrills);
Log.w("add", "" + subDrils);
Log.w("add", "" + mulDrills);
Log.w("add", "" + divDrills);
}
}
Here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simplemathgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme" >
<activity
android:name="com.simplemathgame.Splash"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.MainActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.simplemathgame.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.simplemathgame.GamePlayActivity"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
</application>
</manifest>
here is the logCat:
12-21 22:05:53.949: D/dalvikvm(610): GC_EXTERNAL_ALLOC freed 42K, 53% free 2546K/5379K, external 1917K/2137K, paused 41ms
12-21 22:06:32.809: D/AndroidRuntime(610): Shutting down VM
12-21 22:06:32.809: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-21 22:06:32.818: E/AndroidRuntime(610): FATAL EXCEPTION: main
12-21 22:06:32.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.GamePlayActivity}: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 22:06:32.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-21 22:06:32.818: E/AndroidRuntime(610): ... 11 more
Here is the layout of the GamePlayActivity:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow
android:layout_marginTop="10dp"
android:gravity="center_vertical" >
<TextView
android:textColor="#FFFFFFFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="Addition Drills:"/>
</TableRow>
</TableLayout>
Why does it crashes?
Have a look here:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
Line 31 of GamePlayActivity.java is this line:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
Since the only thing on this line that you reference a member or method of is numberOfAddDrills, then it must be null.
Consider that you are checking for exceptions when using findViewById; however, if the view is not found it will just return null, not throw an exception.
Have a look at the activity_game_play.xml you posted; there is no TextView with android:id="#+id/add_drills_number". You need to create it, and the same deal for the other five TextViews.
Oh, and a hint: Don't just catch generic exceptions with } catch (Exception e1) {, especially if you have no intent of reading the logs. I can't tell you how many times people have missed errors from doing this.
If you want to pass values from another layout, you have two options: keep a reference to the View objects (this is a bit silly, don't do this); or pass the data you need from them to your new Activity:
startGameDrill.putExtra("myIntExtra", 5); // For example
Then, in GamePlayActivity.onCreate(), you can fetch it using getIntent()'s extras:
Bundle extras = getIntent().getExtras();
int myIntExtra = extras.getInt("myIntExtra");
Then use that value instead of relying on the view in the previous layout. There is a fuller example in this answer.
Follow the stacktrace until you find some references to your classes:
It shows:
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31)
So you are creating a NullPointerException at line 31 in GamePlayActivity.java
Line 31:
addDrills = Integer.parseInt((String) numberOfAddDrills.getText());
so maybe numberOfAddDrills is null?