java.lang.IllegalStateException: cannot find button's onClick method in Android Studio - java

I am building an app named Ping in Android Studio. So far my activities are LoginActivity ProfileActivity and Timeline. My issue is that a button in the layout corresponding to the Timeline activity has an onClick method that isn't working. When the button is clicked, the emulator gives the "Unfortunatley, Ping has stopped." I'm defining the buttons and onClick methods the same way I have for other buttons whose functions are working, just this one does not seem to work. I'm receiving an error saying the method can't be found, but I've written the method in the corresponding activity. Here is the logcat:
04-30 10:40:08.727 2075-2075/com.ping_social.www.ping E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ping_social.www.ping, PID: 2075
java.lang.IllegalStateException: Could not find a method onProfilePress(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'profileButton'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: onProfilePress [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Here is my Timeline activity class:
package com.ping_social.www.ping;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class TimeLine extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_line);
/*print log that shows we've got here*/
Log.i("LoginActivity", "Layout has been set");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_time_line, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*called when user presses the Log in button*/
public void onProfilePress(View view){
/*Log the button press*/
Log.i("TimeLine", "Has reached the onProfilePress method");
Intent intent = new Intent(this, ProfileActivity.class);
startActivity(intent);
}
}
And here is my Timeline layout xml code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:theme="#style/GeneralTheme"
tools:context="com.ping_social.www.ping.TimeLine">
<TextView android:text="#string/no_pings" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textIsSelectable="false"
android:textColor="#color/PING_TOP_BAR_RED"
android:id="#+id/textView4" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/timeline_button"
android:id="#+id/timelineButton"
android:textColor="#color/PING_TOP_BAR_RED"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/new_ping_button"
android:id="#+id/newPingButton"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/activity_button"
android:id="#+id/activityButton"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/profile_button"
android:id="#+id/profileButton"
android:layout_weight="1"
android:onClick="onProfilePress"/>
</LinearLayout>
</RelativeLayout>
I'm pretty positive there are not spelling issues, and there are also no other buttons that share the same ID or methods that share the same name. Been stuck on this for a few days, any help is very much appreciated!

Ok, so I made my own test. I've put together a basic relative layout with a single button, put android:theme="#style/AppTheme" in it, and a button - app crashed with the same error. Then I removed android:theme attribute - onclick event fired as it should.
All the same happened when I used AppCompatActivity instead of now-deprecated ActionBarActivity.
It's hard for me to say why it doesn't work with android:theme. It's one of Lollipop's features, but I tried to launch in on API 5.0 emulator. The article states that currently this attribute is only supported for android.support.v7.widget.Toolbar.

Related

I have just started android development and when i was making this app it didnt work while debugging

This app is all about clicking a Button and there is a TextBox which changes its content when clicked.
MainActivity.java
package com.lost.eventhandling;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button willybutton=(Button)findViewById(R.id.willybutton);
assert willybutton != null;
willybutton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
TextView willytext=(TextView)findViewById(R.id.willytext);
willytext.setText("I DID IT ");
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Now for xml code I had
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.lost.eventhandling.MainActivity"
android:background="#71f8bd">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/willy_text"
android:id="#+id/willytext"
android:textColor="#e83858"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="179dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/willy_button"
android:id="#+id/willybutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#764045"/>
</RelativeLayout>
Whenever I debug my application it sends a message it unfortuantely stopped.
And my android monitor gives me this red coloured text:
FATAL EXCEPTION: main
Process: com.lost.eventhandling, PID: 2449
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lost.eventhandling/com.lost.eventhandling.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.lost.eventhandling.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Replace yours OnclickListener code of Button with these lines of code:
Button willybutton=(Button)findViewById(R.id.willybutton);
TextView willytext=(TextView)findViewById(R.id.willytext);
willybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/////PERFORM YOURS OPERATION HERE
}
});

Java error on android studio [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
i am new to android app developing i am following buckys tutorials
currently i am on this part
https://www.youtube.com/watch?v=MdOxtws4DsI&index=13&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
i did everything as told but when i run the app i find this error here's my logcat
12-08 20:42:45.548 2465-2465/com.kimdevelopment.kimmmm I/art: Not late-enabling -Xcheck:jni (already on)
12-08 20:42:45.549 2465-2465/com.kimdevelopment.kimmmm I/art: Late-enabling JIT
12-08 20:42:45.552 2465-2465/com.kimdevelopment.kimmmm I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-08 20:42:45.630 2465-2465/com.kimdevelopment.kimmmm W/System: ClassLoader referenced unknown path: /data/app/com.kimdevelopment.kimmmm-1/lib/x86
12-08 20:42:45.696 2465-2465/com.kimdevelopment.kimmmm D/AndroidRuntime: Shutting down VM
12-08 20:42:45.696 2465-2465/com.kimdevelopment.kimmmm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kimdevelopment.kimmmm, PID: 2465
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kimdevelopment.kimmmm/com.kimdevelopment.kimmmm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at com.kimdevelopment.kimmmm.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-08 20:42:54.014 2465-2465/? I/Process: Sending signal. PID: 2465 SIG: 9
also here's the java file that i edited
package com.kimdevelopment.kimmmm;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout kimsLayout = new RelativeLayout(this);
Button redButton = new Button(this);
kimsLayout.addView(redButton);
setContentView(kimsLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and finally the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kimdevelopment.kimmmm.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
I haven't used tool bars that much so I could be off, but you're instantiating your tool bar with a R.id which usually corresponds to an XML element.
You don't actually have an element in your XML that references the toolbar. You need an element with an ID "toolbar" maybe like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kimdevelopment.kimmmm.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/blue"
android:foreground="?android:attr/windowContentOverlay"
app:theme="#style/Toolbar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
EDIT:
Also, with these lines, you are setting the Activity content to something besides your XML defined layout:
RelativeLayout kimsLayout = new RelativeLayout(this);
Button redButton = new Button(this);
kimsLayout.addView(redButton);
setContentView(kimsLayout);
You should change your XML main RelativeLayout and give it an id (like the elements within it) and then set the content view like this:
setContentView(R.layout.main_layout);

TabHost addTabs on Android crashes the apk

I have a problem that I can not solve, the problem TabHost that crashes when apk run,I've been trying hard for this but still can not, please help.
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
android:layout_gravity="bottom"></TabWidget>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:id="#android:id/tabcontent"></FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
MainActivity.java
package com.example.asus.smartcity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabhostku = (TabHost)findViewById(android.R.id.tabhost);
tabhostku.setup();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, MapActivity.class);
spec = tabhostku.newTabSpec("map");
spec.setIndicator("map",null);
spec.setContent(intent);
tabhostku.addTab(spec);
intent = new Intent().setClass(this, PlaceActivity.class);
spec = tabhostku.newTabSpec("place");
spec.setIndicator("place",null);
spec.setContent(intent);
tabhostku.addTab(spec);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MapActivity.java
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Asus on 11-Nov-15.
*/
public class MapActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
}
}
PlaceActivity.java
package com.example.asus.smartcity;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
/**
* Created by Asus on 11-Nov-15.
*/
public class PlaceActivity extends ListActivity {
String []placeku={"KBS","Surabaya Carnival","Kebun Bibit"};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.place);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,placeku));
}
}
Logcat
11-11 12:51:22.153 19591-19591/? I/art﹕ Late-enabling -Xcheck:jni
11-11 12:51:22.223 19591-19591/com.example.asus.smartcity W/ResourceType﹕ For resource 0x01040529, entry index(1321) is beyond type entryCount(1)
11-11 12:51:22.287 19591-19591/com.example.asus.smartcity W/ResourceType﹕ For resource 0x0104007b, entry index(123) is beyond type entryCount(1)
11-11 12:51:22.288 19591-19591/com.example.asus.smartcity W/ResourceType﹕ For resource 0x01040077, entry index(119) is beyond type entryCount(1)
11-11 12:51:22.314 19591-19591/com.example.asus.smartcity W/ResourceType﹕ For resource 0x0104007b, entry index(123) is beyond type entryCount(1)
11-11 12:51:22.318 19591-19591/com.example.asus.smartcity D/AndroidRuntime﹕ Shutting down VM
11-11 12:51:22.320 19591-19591/com.example.asus.smartcity E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.asus.smartcity, PID: 19591
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.smartcity/com.example.asus.smartcity.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
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:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:754)
at android.widget.TabHost.setCurrentTab(TabHost.java:420)
at android.widget.TabHost.addTab(TabHost.java:247)
at com.example.asus.smartcity.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            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:945)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
11-11 12:51:25.717 19591-19591/com.example.asus.smartcity I/Process﹕ Sending signal. PID: 19591 SIG: 9
You have to change extends AppCompatActivity to ActivityGroup After that change
tabhostku.setup();
to
tabhostku.setup(this.getLocalActivityManager());
Try this,
Change,
tabhostku.setup();
to
mlam = new LocalActivityManager(this, false);
tabhostku.setup(mlam );
And in onPause and onResume add the following code.
#Override
public void onPause() {
super.onPause();
try {
mlam.dispatchPause(isFinishing());
} catch (Exception e) {}
}
#Override
public void onResume() {
super.onResume();
try {
mlam.dispatchResume();
} catch (Exception e) {}
}

Android could not find method in Activity class

I'm following the instructions on the Android developers site for creating your first app. The first few platform tests worked fine for me, but it goes south when trying to use an intent to pass a message from one activity to another. Specifically, when I press the text field to enter input, the app immediately crashes (see intended behavior at https://developer.android.com/training/basics/firstapp/starting-activity.html#DisplayMessage).
A problem that seems to be the same as mine was reported here over a year ago, with no satisfactory answer: Could not find a method sendMessage(View) in the activity class
A couple things I am fairly certain are not the problem:
extends ActionBarActivity -- I know this is deprecated but it worked fine before this bug.
hardcoded onClick in xml -- I have heard this is not best practice, but I doubt the official Android tutorial would instruct it if it didn't at least work.
My own basic and ill-informed diagnosis is that even though I wrote the method sendMessage(View view) in MyActivity.java, the runtime doesn't think it's there. I am lost as to why. Here's my code:
MyActivity.java:
package org.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "org.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** 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);
}
}
activity_my.xml:
<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_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message"
android:onClick="sendMessage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"/>
</LinearLayout>
DisplayMessageActivity.java:
package org.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get message from intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
//create text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//set text view as 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Crash output:
--------- beginning of crash
05-30 11:41:37.734 23198-23198/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.benwiley.myfirstapp, PID: 23198
java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class android.support.v7.internal.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatEditText with id 'edit_message'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Thanks so much in advance. I'm pretty lost. P.S. I am using API 22 with Build Tools 22.0.1.
i think you have many mistakes
activity_my.xml
<button
android:id="#+id/btnsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
in MyActivity.java:
Button button = (Button).findViewById(R.id.btnsend);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_message);
sendMessage(v);}
});
in AndroidManifest.xml :
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message">
While following the same tutorial I had made the same mistake in the activity_my.xml file, by putting the attribute android:onClick="sendMessage" in the EditText element.
It should be in the Button element, of course! The final activity_my.xml should look like this:
<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_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
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>

NullPointerException on TextView using setText()

I'm rather new to android programming, and am running into my old friend the NullPointerException...
I've been on it for quite a while now, but can't figure out what is wrong.
basicly gives me a NullPointerException when I try to call any .setText() methods, maybe someone sees what I'm doing wrong here...(though i tried to follow examples as close as possible)
public class lessonView extends Activity {
TextView adressOfLecture;
TextView lecturer;
TextView lesson;
Lecture lecture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson_view);
Bundle data = getIntent().getExtras();
lecture = (Lecture) data.getParcelable("student");
adressOfLecture = (TextView)findViewById(R.id.lectureViewAdressLabel);
lecturer = (TextView)findViewById(R.id.lectureViewLecturerLabel);
lesson = (TextView)findViewById(R.id.lectureViewTitle);
updateLabels();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_lesson_view, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void updateLabels(){
adressOfLecture.setText(lecture.getRoom());
lecturer.setText(lecture.getTutor());
lesson.setText(lecture.getName());
}
}
also, here's my xml file:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.coronarip7.app.stupla.lessonView">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(ort)"
android:id="#+id/lectureViewAdressLabel"
android:layout_centerVertical="true"
android:layout_toStartOf="#+id/lectureViewTitle"
android:layout_marginRight="86dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dozent"
android:id="#+id/lectureViewLecturerLabel"
android:layout_toEndOf="#+id/lectureViewTitle"
android:layout_alignTop="#+id/lectureViewAdressLabel"
android:layout_alignParentEnd="true"
android:layout_marginRight="27dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lectureViewTitle"
android:gravity="center_horizontal"
android:text="test"
android:textSize="40dp"
android:textStyle="bold"
android:padding="10dp"
android:layout_row="0"
android:layout_column="0"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
and the logcat I'm getting:
04-25 01:23:51.058 12236-12236/com.coronarip7.app.stupla E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.coronarip7.app.stupla, PID: 12236
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coronarip7.app.stupla/com.coronarip7.app.stupla.lessonView}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.coronarip7.app.stupla.lessonView.updateLabels(lessonView.java:59)
at com.coronarip7.app.stupla.lessonView.onCreate(lessonView.java:31)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
seriously, I am out of ideas on how to fix it...
First check if you properly put the extra in the intent. Then I would use the following test in your onCreate method:
Intent intent = getIntent();
if (intent.hasExtra("student")){
lecture = (Lecture) intent.getParcelableExtra("student");
}
Then test if lecture is null like Rami wrote earlier.
Ok guys, thanks for the suggestions, but I'll go the global route. I'll create a static version of my array (from which i wanted to pass an object via the Intent), and just pass an int[] array with the coordiantes and call it in the new view.
But thanks for the quick answers anyways!
(I'm new to stackoverflow, is there a way to mark a question as "solved", or "not-needed-to-be-solved-anymore"?)
Do not use static objects in your code like arrays, objects, they have globally available, you should create intent and add your data into your intent like and call your activity
Intent intent = new Intent ();
intent.putExtra ("student",value);
start activity with intent
And in your
lessonViewActivity check for intent like
Intent intent = getIntent();
if (intent.hasExtra("student")){
lecture = (Lecture) intent.getParcelableExtra("student");
if (lecture !=null){
updateLabels ();
}
}

Categories