First program causing loads of errors - java

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.

Related

Android application crashes after launch. Button issue?

I got problem with my first Android application. Just after running, my app crashes in emulator with "Unfortunately has stopped." message. I checked many threads on Stack and I read pretty much of official documentation and I really do not know what's a cause...
I use AVD with Android 4.4.2, API Level 19. CPU/ABI - ARM. Use Host GPU - checked (without this one emulator works very slowly).
My app should play a song after pressing the button. I would be really grateful for any ideas. Cheers.
Java file:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.os.Build;
import android.media.MediaPlayer;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Button one = (Button) findViewById(R.id.button1);
final MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.elements6);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.start();
}
});
}
#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;
}
}
}
And XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Errors:
05-14 17:29:21.740: D/AndroidRuntime(1145): Shutting down VM
05-14 17:29:21.740: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb2abcba8)
05-14 17:29:21.750: E/AndroidRuntime(1145): FATAL EXCEPTION: main
05-14 17:29:21.750: E/AndroidRuntime(1145): Process: com.example.beatzlooper, PID: 1145
05-14 17:29:21.750: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 17:29:21.750: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.example.beatzlooper.MainActivity.onCreate(MainActivity.java:31)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Activity.performCreate(Activity.java:5231)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-14 17:29:21.750: E/AndroidRuntime(1145): ... 11 more
You are working with fragments.
The Fragment cannot be added to the R.id.container View because it doesn't exists.
Just delete the whole if-clause in the onCreate() and also delete the Placeholder Class you don't need it actually.
Comment this code
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/

Could not find a method sendMessage(View) in the activity class

MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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_display_message,
container, false);
return rootView;
}
}
}
Error Log
04-10 01:37:00.460: W/dalvikvm(1650): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
04-10 01:37:00.520: E/AndroidRuntime(1650): FATAL EXCEPTION: main
04-10 01:37:00.520: E/AndroidRuntime(1650): Process: com.example.myfirstapp, PID: 1650
04-10 01:37:00.520: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3810)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4438)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18422)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:136)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 01:37:00.520: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getMethod(Class.java:857)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3803)
04-10 01:37:00.520: E/AndroidRuntime(1650): ... 11 more
04-10 01:37:04.820: I/Process(1650): Sending signal. PID: 1650 SIG: 9
I've tried to look at other questions answered similar to mine, but I can't find an answer that seems to help my situation. Can anyone help?
I was struggling with a similar error following the android tutorial
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 18300
java.lang.IllegalStateException: Could not find method sendMessage (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
but in my case the WYSIWYG editor (IntelliJ w/Android Studio plugin) ended up populating the onClick property in activity_main.xml with some extra junk about MainActivity:
android:onClick="sendMessage (MainActivity)"
So I deleted " (MainActivity)" and it stopped crashing. I see this is different from your problem as your xml file already seems correct with android:onClick="sendMessage". But wanted to add it here for any others struggling with what I saw. This was the closest post to the issue I was seeing. I'm just getting started and this was killing me, so hope it helps someone else.
The problem is the onClick property in one of your Button tags:
android:onClick="sendMessage" />
Just make sure you have a method sendMessage(View) in your Activity.
It is because you need to implement implements android.view.View.OnClickListener in your class, therefore add the correct imports i.e. import android.view.View.

I cannot get this class to open another class/xml via button

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>

Why does my app "unfortunately stop". All this I want is some simple buttons

This is my very first app. All I need is five buttons, two that call certain phone numbers (only have created one so far) and three buttons that take the user to a certain URL. I have no errors or warnings or any direction on how to navigate the LogCat.
LogCat:
10-08 14:41:40.716: D/AndroidRuntime(793): Shutting down VM
10-08 14:41:40.716: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-08 14:41:40.777: E/AndroidRuntime(793): FATAL EXCEPTION: main
10-08 14:41:40.777: E/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{acps.mhs/acps.mhs.MainActivity}: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.os.Looper.loop(Looper.java:137)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): at java.lang.reflect.Method.invoke(Method.java:525)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-08 14:41:40.777: E/AndroidRuntime(793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:41:40.777: E/AndroidRuntime(793): at dalvik.system.NativeStart.main(Native Method)
10-08 14:41:40.777: E/AndroidRuntime(793): Caused by: java.lang.NullPointerException
10-08 14:41:40.777: E/AndroidRuntime(793): at acps.mhs.MainActivity.onCreate(MainActivity.java:25)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Activity.performCreate(Activity.java:5133)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 14:41:40.777: E/AndroidRuntime(793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-08 14:41:40.777: E/AndroidRuntime(793): ... 11 more
10-08 14:42:19.696: I/Process(793): Sending signal. PID: 793 SIG: 9
Mainactivity.java
package acps.mhs;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button mhshome, pp, mhsdir, cmhs;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
mhshome = (Button) findViewById(R.id.mhshome);
pp = (Button) findViewById(R.id.pp);
mhsdir = (Button) findViewById(R.id.mhsdir);
cmhs = (Button) findViewById(R.id.cmhs);
mhshome.setOnClickListener(this);
pp.setOnClickListener(this);
mhsdir.setOnClickListener(this);
cmhs.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.mhshome:
Uri uri = Uri.parse("http://www2.k12albemarle.org/school/mohs/Pages/default.aspx");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case R.id.pp:
Uri uri2 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(intent2);
break;
case R.id.mhsdir:
Uri uri3 = Uri.parse("http://www2.k12albemarle.org/school/MOHS/Pages/Directory.aspx");
Intent intent3 = new Intent(Intent.ACTION_VIEW, uri3);
startActivity(intent3);
break;
case R.id.cmhs:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567890"));
startActivity(callIntent);
break;
}
}
#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;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/pp"
style="#style/AppBaseTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhshome"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="#string/pp"/>
<Button
android:id="#+id/mhshome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:onClick="onClick"
android:text="#string/mhshome" />
<Button
android:id="#+id/mhsdir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/mhsdir" />
<Button
android:id="#+id/cmhs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mhsdir"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/callmhs" />
</RelativeLayout>
Its gonna be something incredibly simple I'm certain. Please point me in the right direction.
You need to call setContentView(R.layout.activity_main) :) That's why findViewById is returning null on your views.
You are not setting your layout.
Call setContentView(layout) in your onCreate() method.

Android App Crashes on Button Click

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.

Categories