Android studio crossfade drawables crashes app - java

I am trying to make basic app that crossfades one image for another and then back again.
The app however keeps crashing when I click the first image.
The crossfade method fade works on it's own. It has just begun crashing since I added the second method to transition the images back.
I see the InvocationTargetException and the NullPointerException but I don't understand how/why this is happening.
Also the onClick within the content setup is set to the correct methods.
package com.example.richardcurteis.layoutdemo;
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.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView obama;
ImageView will;
public void fade(View view) {
obama = (ImageView) findViewById(R.id.obama);
will = (ImageView) findViewById(R.id.will);
obama.animate().alpha(0f).setDuration(2000);
will.animate().alpha(1f).setDuration(2000);
}
public void fadeBack(View view) {
will.animate().alpha(1f).setDuration(2000);
obama.animate().alpha(0f).setDuration(2000);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
}
Activity Main xml:
<?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.example.richardcurteis.layoutdemo.MainActivity"
tools:showIn="#layout/activity_main"
android:clickable="false">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/obama"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:src="#drawable/obama"
android:onClick="fade" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/will"
android:layout_alignTop="#+id/obama"
android:layout_alignParentStart="true"
android:src="#drawable/will"
android:alpha="0"
android:onClick="fadeBack" />
</RelativeLayout>
Errors:
12-29 17:17:32.003 3368-3368/com.example.richardcurteis.layoutdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.richardcurteis.layoutdemo, PID: 3368
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
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 'android.view.ViewPropertyAnimator android.widget.ImageView.animate()' on a null object reference
at com.example.richardcurteis.layoutdemo.MainActivity.fadeBack(MainActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
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) 

You are initializing views obama and will only in fade(). If fadeBack() is called before fade() values of obama and will are both null. That's why you are getting a null pointer exception.
Move this code from fade() to onCreate(),
obama = (ImageView) findViewById(R.id.obama);
will = (ImageView) findViewById(R.id.will);
So that it will get initiated at the time of view creation itself.
MainActivity should look like this :
public class MainActivity extends AppCompatActivity {
ImageView obama;
ImageView will;
public void fade(View view) {
obama.animate().alpha(0f).setDuration(2000);
will.animate().alpha(1f).setDuration(2000);
}
public void fadeBack(View view) {
will.animate().alpha(1f).setDuration(2000);
obama.animate().alpha(0f).setDuration(2000);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
obama = (ImageView) findViewById(R.id.obama);
will = (ImageView) findViewById(R.id.will);
......
}
......
}

Related

How use Android webview in fragment

HomeFragment.java
package com.example.starcitizen.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import androidx.fragment.app.Fragment;
import com.example.starcitizen.R;
public class HomeFragment extends Fragment {
public HomeFragment(){ }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
String url = "http://google.com";
WebView view = (WebView) rootView.findViewById(R.id.home_webview);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return rootView ;
}
}
fragment_home
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.starcitizen.fragment.HomeFragment">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home_webview"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I looked at other questions. but my app is not works.
So I wonder if I need to fix my main activity. So I attached the main activity.
MainActivity.java
package com.example.starcitizen;
import android.os.Bundle;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_guide, R.id.nav_maps,
R.id.nav_ships, R.id.nav_weapons, R.id.nav_mission)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
#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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Sorry I'm not good at english. So with help of a translator...
and I asked stackoverflow for the first time.
I'm working on an app for koreans. but I have some problem in fragment.
If a webview is entered, App will be turned off without running.
please help me.
add logcat!
11-15 20:00:31.465 3666-3666/com.example.starcitizen E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.starcitizen, PID: 3666
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.starcitizen/com.example.starcitizen.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.webkit.WebView
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.webkit.WebView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.example.starcitizen.fragment.HomeFragment.onCreateView(HomeFragment.java:18)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
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:905) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
at com.example.starcitizen.fragment.HomeFragment.onCreateView(HomeFragment.java:18) 
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100) 
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874) 
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830) 
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613) 
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904) 
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613) 
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246) 
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542) 
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236) 
at android.app.Activity.performStart(Activity.java:6006) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288) 
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:905) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
at android.content.res.Resources.getText(Resources.java:299)
at android.content.res.Resources.getString(Resources.java:385)
at com.android.org.chromium.content.browser.ContentViewCore
Tried to understand but failed..
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
There is no difference to use WebView in Activity and Fragment.
I can't figure your troubleshooting but I recommend you to check the following things.
check the permission in manifest
<uses-permission android:name="android.permission.INTERNET" />
check if there is Runtime Exception in onAttach() of HomeFragment.
If you wanna see the WebView in Fragment, you should declare the following code.
view.setWebViewClient(new WebViewClient());
Update
final String source = "http://google.com";
final WebView webView = (WebView) rootView.findViewById(R.id.home_webview);
//webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//Do some like progress bar loading here.
}
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl(source);
}
});

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>

Categories