Fragment TextView ID Causing Crash - java

I am mainly looking for clarification and advice on this. In the app I am working on I setup a fragment for handling some basic controls and text, and moved the necessary XML from the activities layout file into the fragments layout file, without making any changes. However now when I run the app I get a NULLPOINTEREXCEPTION and it seems to be caused at line 19 in my GameStart activty when I try to set the textView by its ID. I have not changed the ID, only moved it to the fragment layout file, so I have two questions:
1) What exactly is the reason this is happening, because something similar happened before that I resolved but I don't see the reason it was happening in the reference docs on Android Developers
2) Any advice on how to fix this
Below is the GameStart activity, LogCat with the error, and the XML files in question. Thanks in advance, I've spent 2 hours now trying to find this answer.
EDITED FILES/LOGCAT BELOW
LogCat:
12-04 13:48:06.322 826-826/com.saphiric.simproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.saphiric.simproject/com.saphiric.simproject.GameStart}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.saphiric.simproject.GameStart.<init>(GameStart.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Starting Activity (The error occurs when the app tries to start the GameStart activity):
package com.saphiric.simproject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class TitleMenu extends ActionBarActivity {
public void startGame(View view){
Intent gameStart = new Intent(this, GameStart.class);
startActivity(gameStart);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_main_menu);
}
}
Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Relative layout to handle main interaction area -->
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:background="#drawable/bg_text">
<TextView
android:id="#id/storyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/story_opening_one"
android:textColor="#color/black" />
<Button
android:id="#+id/advanceButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/controlsFragment"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/button_advance"
android:onClick="advanceGame" />
<Button
android:id="#+id/menuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#id/advanceButton"
android:background="#drawable/menu_button" />
</RelativeLayout>
</RelativeLayout>
Fragment Class:
package com.saphiric.simproject;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Saphiric on 12/4/14.
*/
public class ControlsFragment extends Fragment {
/**
* Fragment Lifecycle Methods
*/
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// Inflates the layout resource for the fragment
return inflater.inflate(R.layout.controls_fragment, container, false);
}
#Override
public void onPause(){
}
}
GameStart activity:
package com.saphiric.simproject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
* Created by Saphiric on 11/12/14.
*/
public class GameStart extends Activity{
/**
* Necessary activity variables
*/
protected int clickCount = 0;
TextView storyText = (TextView) findViewById(R.id.storyText);
/**
* Sets up and alert dialogue builder for making decisions
* This will need to be revised during production
*/
AlertDialog.Builder decisionTime = new AlertDialog.Builder(getApplicationContext());
/**
* Handles advancing non character based conversations at the beginning
* of the story.
* If the player decides to investigate the scream, then the app loads the appropriate activity, and the same with the
* decision to leave the scene.
*/
public void advanceGame(View view){
clickCount = clickCount + 1; // Increments clickCount by 1 per click
if (clickCount == 1){
storyText.setText(R.string.story_opening_two);
} else if(clickCount == 2){
decisionTime.setTitle("Decision Time!"); // Sets the title for the decision box
decisionTime.setCancelable(false); // Sets it so that the decision can not be cancelled.
decisionTime.setPositiveButton("Investigate",new DialogInterface.OnClickListener() { // Sets up the positive button
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
decisionTime.setNegativeButton("Leave the scene.",new DialogInterface.OnClickListener() { // Sets up the negative button
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
}
/**
* Android activity methods
* #param savedInstanceState is the apps savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_start);
}
}
GameStart Layout XML:
<?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"
android:background="#drawable/bg_school_gate"
android:orientation="vertical">
<fragment
android:id="#+id/controlsFragment"
android:name="com.saphiric.simproject.ControlsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/controls_fragment" />
</RelativeLayout>

No, you don't have any fragment here, read more on How to use fragments.
But that's not your problem it should work the way you did it.
The only problem is that you're calling findViewById in your constructor, or even before that.
findViewById only works if you have a UI, so you must do all UI initialization after setContentView in onCreate!
TextView storyText;
...
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_start);
storyText = (TextView) findViewById(R.id.storyText);
}
Also don't forget to call show() on decisionTime.

TextView storyText = (TextView) findViewById(R.id.storyText);
This should only be called after you set your contentview.
The final result should be:
TextView storyText;
And then on onCreate
setContentView(R.layout.activity_game_start);
storyText = (TextView) findViewById(R.id.storyText);

1) The reason this is happening is that the view has not yet been instantiated at the time you are trying to get it's reference.
2) How to fix it: if you have a Fragment, then you should get a reference to it's View's in the Fragment.onCreateView() method. Then you can setup the Activity to talk to the Fragment to do whatever changes you need to the View.

Related

android device showing "My application keeps stoping "

mainactivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private WebView mywebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.webView);
WebSettings webSettings=mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.loadUrl("file:///android_asset/myresource.html");
mywebview.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (mywebview.canGoBack()) {
mywebview.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"
tools:ignore="ExtraText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<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=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>12/13 23:40:42: Launching 'app' on Physical Device.
Install successfully finished in 14 s 668 ms.
$ adb shell am start -n "com.example.myapplication/com.example.myapplication.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 25636 on device 'xiaomi-redmi_7a-2c5b9bbe0406'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
I/FeatureParser: can't find pine.xml in assets/device_features/,it may be in /system/etc/device_features
E/Perf: Fail to get file list com.example.myapplication
E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
Fail to get file list com.example.myapplication
getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 25636
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.SplashActivity}: android.content.res.Resources$NotFoundException: Drawable com.example.myapplication:drawable/background with resource ID #0x7f07005f
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3304)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3443)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:221)
at android.app.ActivityThread.main(ActivityThread.java:7520)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.myapplication:drawable/background with resource ID #0x7f07005f
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/background.xml from drawable resource ID #0x7f07005f
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:898)
at android.content.res.ResourcesImpl.originalLoadDrawable(ResourcesImpl.java:679)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:586)
at android.content.res.MiuiResourcesImpl.loadDrawable(MiuiResourcesImpl.java:307)
at android.content.res.Resources.getDrawableForDensity(Resources.java:920)
at android.content.res.Resources.getDrawable(Resources.java:859)
at android.content.Context.getDrawable(Context.java:696)
at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:454)
at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:144)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:411)
at androidx.appcompat.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:86)
at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow(AppCompatDelegateImpl.java:647)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureWindow(AppCompatDelegateImpl.java:623)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreate(AppCompatDelegateImpl.java:350)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:105)
at com.example.myapplication.SplashActivity.onCreate(SplashActivity.java:11)
at android.app.Activity.performCreate(Activity.java:7893)
at android.app.Activity.performCreate(Activity.java:7880)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3279)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3443)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:221)
at android.app.ActivityThread.main(ActivityThread.java:7520)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/AndroidRuntime: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:278)
at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:199)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:144)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1402)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1361)
at android.content.res.ResourcesImpl.createFromXmlForDensity(ResourcesImpl.java:1615)
at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:963)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:882)
... 30 more
I/Process: Sending signal. PID: 25636 SIG: 9
**splashactivity.java**
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
I think you have problems in your splashActivity not the main activity look for drawables that you use for your splash xml file you must import them correctly in your
res->drawable folder of your project weather they are vectors or pngs. Just try to import them in the right place.If it doesnt solve the the problem please put your splash xml file so I can help you.
Update:
Look at your splashactivity it doesnt have any content view
You must create layout for your splash activity .
Go to your project packages expand res folder then right click on layout and create a layout resource then name it as you prefere for example splash with lowercase then in your splash activity under oncreate method add:
setContentView(R.layout.splash.xml);
Put this line right after super.onCreate(savedinstancestate)

You must call removeView() on the child's parent first! How to get rid of this error? [duplicate]

This question already has answers here:
Call removeView() on the child's parent first
(11 answers)
Closed 6 years ago.
I have made this simple app that uses explicit intents to move from an activity to another.
It works fine for all activities (e.g. move from MainActivity to FamilyActivity) but it doesn't wok when it comes to move from MainActivity to NumbersActivity and it show this LogCat :
09-05 08:04:02.518 26820-26820/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5400)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3430)
at android.view.ViewGroup.addView(ViewGroup.java:3301)
at android.view.ViewGroup.addView(ViewGroup.java:3246)
at android.view.ViewGroup.addView(ViewGroup.java:3222)
at com.example.android.miwok.NumbersActivity.onCreate(NumbersActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424) 
at android.app.ActivityThread.access$600(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5400) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 
at dalvik.system.NativeStart.main(Native Method) 
MainActivty.java
package com.example.android.miwok;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the view to use the activity main.xml
setContentView(R.layout.activity_main);
// Find the view that shows the numbers category
//TextView numbers = (TextView)findViewById(R.id.numbers);
// Find the view that shows the numbers category, then set a clickListener on it
TextView numbers= (TextView)findViewById(R.id.family);
numbers.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, NumbersActivity.class);
startActivity(i);
}
});
// Repeat the same for other categories
TextView family= (TextView)findViewById(R.id.family);
family.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, FamilyMembersActivity.class);
startActivity(i);
}
});
TextView colors = (TextView)findViewById(R.id.colors);
colors.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, ColorsActivity.class);
startActivity(i);
}
});
TextView phrases = (TextView)findViewById(R.id.phrases);
phrases.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, PhrasesActivity.class);
startActivity(i);
}
});
}
}
NumbersActivity.java
package com.example.android.miwok;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
}
}
MainActivity.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:background="#color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:background="#color/category_numbers"
android:text="#string/category_numbers" />
<TextView
android:id="#+id/family"
style="#style/CategoryStyle"
android:background="#color/category_family"
android:text="#string/category_family" />
<TextView
android:id="#+id/colors"
style="#style/CategoryStyle"
android:background="#color/category_colors"
android:text="#string/category_colors" />
<TextView
android:id="#+id/phrases"
style="#style/CategoryStyle"
android:background="#color/category_phrases"
android:text="#string/category_phrases" />
</LinearLayout>
NumbersAtivity.xml
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.miwok.NumbersActivity">
</RelativeLayout>
You're programmatically adding a View somewhere and that view has already been added somewhere else.
This error occurs most often inside for-loops where the same View used inside each iteration.

Android app unable to start activity componentinfo android.content.res.Resources$NotFound

Here's the entire logcat stack trace:
08-23 02:19:52.826 20628-20628/com.example.sham_tech.lastchance E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sham_tech.lastchance, PID: 20628
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sham_tech.lastchance/com.example.sham_tech.lastchance.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
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:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
at android.content.res.Resources.loadDrawable(Resources.java:2152)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawable Manager.java:181)
at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java: 77)
at android.support.v7.app.AppCompatDelegateImplBase.<init> (AppCompatDelegateImplBase.java:83)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
at
com.example.sham_tech.lastchance.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)        
I cannot understand what's the problem!.
Here's the activity_main.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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView android:text="Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
/>
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginBottom="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="SubmitOrder"
/>
</LinearLayout>
Is there anything wrong in the code?
MainActivity.java
package com.example.sham_tech.lastchance;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
display(1);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
}
I have tried to solve it for more than 4 days and there is no result. Please help me!
The problem is that it's looking for a file named "abc_ic_ab_back_material.xml" in your res/drawable/ directory and it's not finding it. Make sure you have that file in that location.
The code you have pasted here did not have referenced the
abc_ic_ab_back_material
If you have used it in the other code, so find if you have the resource named abc_ic_ab_back_material under res/drawable/, if not found, just get one.
If you did not have used it anywhere, just clean you project and rebuild.

Android countdownTimer error on button click

I want that user will input minutes in the edit text box and after that when the submit button is clicked the counter will start . but what is the error here? it shows that unfortunately the app has stopped. Please help.
package com.example.asifsabir.counterapps;
import android.os.Bundle;
import android.os.CountDownTimer;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
TextView tv1;
EditText et1;
Button bt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
et1 =(EditText) findViewById(R.id.et1);
bt1= (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int min =Integer.parseInt(et1.getText().toString());
if (!et1.getText().toString().isEmpty()){
final int sec = min*60;
new CountDownTimer(sec, 1000) {
public void onTick(long millisUntilFinished) {
tv1.setText("" + String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
tv1.setText("done!");
}
}.start();
}
}
}
);
}
}
and the xml code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/et1"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:inputType="number"
android:hint="enter sec"
android:textAlignment="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bt1"
android:layout_gravity="center"
android:text="submit"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count"
android:layout_marginTop="100dp"
android:layout_gravity="center"
android:textColor="#3559da"
android:textSize="80sp" />
</LinearLayout>
error logs:
339-25339/? I/art: Late-enabling -Xcheck:jni
03-24 05:25:44.927 25339-25339/? I/art: VMHOOK: rlim_cur : 0 pid:25339
03-24 05:25:44.957 25339-25353/? I/art: Debugger is no longer active
03-24 05:25:45.127 25339-25339/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asifsabir.counterapps, PID: 25339
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asifsabir.counterapps/com.example.asifsabir.counterapps.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5702)
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:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.asifsabir.counterapps.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5702) 
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:1029) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 
03-24 05:28:37.087 25339-25339/com.example.asifsabir.counterapps D/Process: killProcess, pid=25339
03-24 05:28:37.087 25339-25339/com.example.asifsabir.counterapps D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
You are trying to convert the EditText value into integer in onCreate() -
int min =Integer.parseInt(et1.getText().toString());
Now in onCreate() , at that time your
EditText will be empty and
et1.getText().toString() will be ""
and hence
Integer.parseInt("") will apparently throw NumberFormatException
To avoid this you need to do 2 most important things -
First check for empty string
if (!et1.getText().toString().isEmpty()){
//convert here
}
Also make your EditText in your xml to accept only numbers so that it doesn't throws NumberFormatException again in future.
you should get Integer.parseInt when click event happens, and your input must be in millis:
bt1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int min = Integer.parseInt(et1.getText().toString());
int sec = min*60;
int millis = sec*1000;
new CountDownTimer(millis, 1000) {
//(...)
And you should also validate before if EditText is not empty, to avoid NumberFormat exceptions like you get.
if (et1.getText().toString().isEmpty())
//notice user to input some number
return;

Button causing app crash on launch

Hi when I launch my app it crashes instantly reading the logcat I can see that it is cause by the sendButton in my mainActivity, from reading around I people say it has to do with the button not being initiated but as far I can see it is, I have it declared in my fragment_main.xml okay and without the button code in MainActivity.java my code loads the app grand. Any help would really be appreciate.
Logcat
06-20 08:50:18.323: E/AndroidRuntime(30036): FATAL EXCEPTION: main
06-20 08:50:18.323: E/AndroidRuntime(30036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.draco.dragonmessage/com.draco.dragonmessage.MainActivity}: java.lang.NullPointerException
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2483)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread.access$600(ActivityThread.java:178)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.os.Handler.dispatchMessage(Handler.java:107)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.os.Looper.loop(Looper.java:194)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread.main(ActivityThread.java:5560)
06-20 08:50:18.323: E/AndroidRuntime(30036): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 08:50:18.323: E/AndroidRuntime(30036): at java.lang.reflect.Method.invoke(Method.java:525)
06-20 08:50:18.323: E/AndroidRuntime(30036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
06-20 08:50:18.323: E/AndroidRuntime(30036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
06-20 08:50:18.323: E/AndroidRuntime(30036): at dalvik.system.NativeStart.main(Native Method)
06-20 08:50:18.323: E/AndroidRuntime(30036): Caused by: java.lang.NullPointerException
06-20 08:50:18.323: E/AndroidRuntime(30036): at com.draco.dragonmessage.MainActivity.onCreate(MainActivity.java:48)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.Activity.performCreate(Activity.java:5135)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
06-20 08:50:18.323: E/AndroidRuntime(30036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
06-20 08:50:18.323: E/AndroidRuntime(30036): ... 11 more
MainActivity.java
package com.draco.dragonmessage;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
String sendMessageString = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
final Button sendMessage = (Button) findViewById(R.id.messageButton);
System.out.println(sendMessage.toString());
sendMessage.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
final TextView receivedMessage = (TextView) findViewById(R.id.textReceived);
final EditText messageToSend = (EditText) findViewById(R.id.editTextSend);
sendMessageString = messageToSend.toString();
receivedMessage.setText(sendMessageString);
}
});
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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 {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.draco.dragonmessage.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editTextSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/section_label"
android:layout_alignParentBottom="true"
android:ems="10"
android:hint="Enter message to send..." />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/section_label"
android:layout_alignTop="#+id/textReceived"
android:text="Keith"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/textReceived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/section_label"
android:layout_alignRight="#+id/editTextSend"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/messageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextSend"
android:layout_alignParentRight="true"
android:layout_marginRight="31dp"
android:text="Button" />
</RelativeLayout>
activity_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.draco.dragonmessage.MainActivity" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<fragment
android:id="#+id/navigation_drawer"
android:name="com.draco.dragonmessage.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
Thanks again for any help.
The Button with ID messageButton is in fragment_main.xml. With findByViewById() you are accessing to activity_main.xml (setContentView(R.layout.activity_main);.
You can add an attribute Button to the class and link the element in the onCreateView method of PlaceholderFragment. Then you can access to this button from anywhere in the activity.
Just Move the following lines to Oncreate() and change R.layout to your fragment layout. You are using a wrong layout
final TextView receivedMessage = (TextView) findViewById(R.id.textReceived);
final EditText messageToSend = (EditText) findViewById(R.id.editTextSend);
below this one
final Button sendMessage = (Button) findViewById(R.id.messageButton);

Categories

Resources