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.
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
package com.example.ysk;
import android.content.Intent;
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;
public class MainActivity extends ActionBarActivity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.example.ysk.EKRANBIR"));
}
});
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;
}
}
}
StackTrace:
04-16 05:51:27.611: E/AndroidRuntime(325): FATAL EXCEPTION: main
04-16 05:51:27.611: E/AndroidRuntime(325): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ysk/com.example.ysk.MainActivity}: java.lang.NullPointerException
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.os.Looper.loop(Looper.java:123)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-16 05:51:27.611: E/AndroidRuntime(325): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 05:51:27.611: E/AndroidRuntime(325): at java.lang.reflect.Method.invoke(Method.java:521)
04-16 05:51:27.611: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 05:51:27.611: E/AndroidRuntime(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 05:51:27.611: E/AndroidRuntime(325): at dalvik.system.NativeStart.main(Native Method)
04-16 05:51:27.611: E/AndroidRuntime(325): Caused by: java.lang.NullPointerException
04-16 05:51:27.611: E/AndroidRuntime(325): at com.example.ysk.MainActivity.onCreate(MainActivity.java:25)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 05:51:27.611: E/AndroidRuntime(325): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-16 05:51:27.611: E/AndroidRuntime(325): ... 11 more
I Think I have problem with the button. I thank for answers already.
Try this..
I guess button is belongs to fragment_main.xml. So you have to initialize the button in PlaceholderFragment.
then you cannot go to another activity using this startActivity(new Intent("com.example.ysk.EKRANBIR")); this will get ActivityNotFoundException
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);
Button button1 = (Button) rootView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getActivity(),EKRANBIR.class));
}
});
return rootView;
}
}
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.
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>
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.