Setting id for root view crashes android app - android

As a quick visualization, this is what my layout looks like:
<android.support.design.widget.CoordinatorLayout 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/main_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="me.smac89.sample.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/cards_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
When I set an ID (android:id="") for the Coordinator layout, the app crashes upon start up, however removing the id allows it to run normally. What is going on? Why does setting an ID for the root view cause the app the crash?
I've seen people using this findViewById(android.R.id.content) to get the root view; is this the only way to obtain the rootview?
Crash log
07-06 18:30:42.253 9385-9385/me.smac89.deloittepixel E/AndroidRuntime:
FATAL EXCEPTION: main
Process: me.smac89.deloittepixel, PID: 9385
java.lang.RuntimeException: Unable to start activity
ComponentInfo{me.smac89.sample/me.smac89.sample.MainActivity}:
java.lang.ClassCastException:
android.support.design.widget.CoordinatorLayout cannot be cast to
android.app.Activity
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.ClassCastException:
android.support.design.widget.CoordinatorLayout cannot be cast to
android.app.Activity
at butterknife.internal.Finder$2.getContext(Finder.java:34)
at butterknife.internal.Finder.getResourceEntryName(Finder.java:131)
at butterknife.internal.Finder.findRequiredViewAsType(Finder.java:86)
at
me.smac89.sample.MainActivity$$ViewBinder$InnerUnbinder.(MainActivity$$ViewBinder.java:32)
at
me.smac89.sample.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:20)
at
me.smac89.sample.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:17)
at butterknife.ButterKnife.bind(ButterKnife.java:122)
at me.smac89.sample.MainActivity.onCreate(MainActivity.java:50)
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)
MainActivity.java
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
public static final String TAG = MainActivity.class.getName();
#BindView(R.id.main_content)
ViewPager viewPager;
#BindView(R.id.main_tab_spinner)
Spinner spinner;
#BindView(R.id.main_tab)
TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setKeyAndSecret();
ButterKnife.bind(this);
spinner.setAdapter(new SpinnerDummyAdapter(this));
initTabs();
viewPager.addOnPageChangeListener(this);
}
...
}

I had a similar issue and solved it with: Build > Clean Project

Related

App crashes when trying to add a new CyborgController using createNewLayerBuilder()

This is what I'm doing in the Application class:
#Override
public void onCreate() {
super.onCreate();
CyborgBuilder.startCyborg(new CyborgConfiguration(this, R.layout.cyborgview__auto_reply, BasicModulePack.class));
}
This is how cyborgview__auto_reply.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<com.nu.art.cyborg.core.CyborgView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cyborg="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
cyborg:controller="com.hedshafran.autoreply.controllers.Controller_PermissionScreen"
cyborg:tag="Controller_Permission"/>
And this is what I'm doing in the Controller_PermissionScreen class that causes the crash:
createNewLayerBuilder().setControllerType(Controller_MainScreen.class).setLayoutId(R.layout.controller__main_screen).build();
That causes the crash, which looks like this:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hedshafran.autoreply, PID: 17910
java.lang.RuntimeException: Unable to resume activity {com.hedshafran.autoreply/com.nu.art.cyborg.core.CyborgActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.nu.art.cyborg.core.CyborgStackController$StackLayerBuilder com.nu.art.cyborg.core.CyborgStackController.createLayerBuilder()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3791)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3832)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.nu.art.cyborg.core.CyborgStackController$StackLayerBuilder com.nu.art.cyborg.core.CyborgStackController.createLayerBuilder()' on a null object reference
at com.nu.art.cyborg.core.CyborgController.createNewLayerBuilder(CyborgController.java:506)
at com.hedshafran.autoreply.controllers.Controller_PermissionScreen.onResume(Controller_PermissionScreen.java:35)
at com.nu.art.cyborg.core.CyborgController.dispatchLifeCycleEvent(CyborgController.java:270)
at com.nu.art.cyborg.core.CyborgActivityBridgeImpl.dispatchLifecycleEvent(CyborgActivityBridgeImpl.java:476)
at com.nu.art.cyborg.core.CyborgActivityBridgeImpl.onResume(CyborgActivityBridgeImpl.java:213)
at com.nu.art.cyborg.core.CyborgActivity.onResume(CyborgActivity.java:155)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1277)
at android.app.Activity.performResume(Activity.java:7088)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3768)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3832) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
How can I avoid that crash and open a new CyborgController the proper way?
Your cyborgview__auto_reply.xml should utilize the CyborgStackConroller that will contain your Controller_PermissionScreen, like so:
<?xml version="1.0" encoding="utf-8"?>
<com.nu.art.cyborg.core.CyborgView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cyborg="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/CV_RootStack"
cyborg:controller="com.nu.art.cyborg.core.CyborgStackController"
cyborg:rootController="com.hedshafran.autoreply.controllers.Controller_PermissionScreen"
cyborg:rootTag="Controller_Permission"
cyborg:tag="RootStack"
/>
Point is you can only use the createNewLayerBuilder api if the controller that calls this method is in a stack. Otherwise use the getControllerById(R.id.CV_RootStack) and create a new layer builder.

Set all the views on an Activity programmatically

First of all, I'm starting at Android, so maybe I'm asking a stupid question. Oh, and sorry for my English.
I have an Activity that receives an Object when it starts (in the previous activity's Intent) and I want to set severals views (ImageView and TextViews) in the Java file. I have looked for the way to do it, and this is what I think I need.
The XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/party"
tools:context=".activities.PartyActivity">
<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:orientation="horizontal"
tools:context=".activities.PartyActivity"
android:id="#+id/head">
<!--Party-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/photo"/>
<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:orientation="vertical"
tools:context=".activities.PartyActivity">
<!--Dia/hora-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"/>
<!--Dirección-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/currentCity"
android:id="#+id/direction"/>
<!--Precio-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/currentCity"
android:id="#+id/price"/>
</RelativeLayout>
</RelativeLayout>
<!--Descripcion-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/head"
android:id="#+id/description"/>
<!--Mapa-->
And this is the class:
public class PartyActivity extends Activity {
private RelativeLayout partyLayout;
private Party party;
private ImageView photoView;
private TextView dateView;
private TextView directionVew;
private TextView priceView;
private TextView descriptionView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_party);
partyLayout = (RelativeLayout)findViewById(R.id.party);
party = (Party)getIntent().getExtras().getSerializable("Party");
setParty();
}
private void setParty(){
partyLayout.removeAllViews();
photoView = (ImageView)findViewById(R.id.photo);
dateView = (TextView)findViewById(R.id.date);
directionVew = (TextView)findViewById(R.id.direction);
priceView = (TextView)findViewById(R.id.price);
descriptionView = (TextView)findViewById(R.id.description);
photoView.setImageBitmap(party.getPhoto());
dateView.setText(Session.getInstance().getDateFormat().format(party.getDate().getTime()));
directionVew.setText(party.getDirection());
priceView.setText(party.getPrice().toString());
descriptionView.setText(party.getDescripcion());
setContentView(photoView);
setContentView(dateView);
setContentView(directionVew);
setContentView(priceView);
setContentView(descriptionView);
}
When I try to go to the activity, the application shuts down. I have debugged it and the Party object has been received correctly, but in the setParty method it throws this exception:
04-08 14:21:08.619 14680-14680/trebolete.keloke E/AndroidRuntime: FATAL EXCEPTION: main
Process: trebolete.keloke, PID: 14680 java.lang.RuntimeException: Unable to start activity ComponentInfo{trebolete.keloke/trebolete.keloke.activities.PartyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' 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 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at trebolete.keloke.activities.PartyActivity.setParty(PartyActivity.java:46)
at trebolete.keloke.activities.PartyActivity.onCreate(PartyActivity.java:33)
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)
If I remove the line partyLayout.removeAllViews(); it throws this exception:
04-09 13:47:44.101 2576-2576/trebolete.keloke E/AndroidRuntime: FATAL EXCEPTION: main
Process: trebolete.keloke, PID: 2576
java.lang.RuntimeException: Unable to start activity ComponentInfo{trebolete.keloke/trebolete.keloke.activities.PartyActivity}: 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: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.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:4309)
at android.view.ViewGroup.addView(ViewGroup.java:4145)
at android.view.ViewGroup.addView(ViewGroup.java:4117)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:423)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:404)
at android.app.Activity.setContentView(Activity.java:2186)
at trebolete.keloke.activities.PartyActivity.setParty(PartyActivity.java:52)
at trebolete.keloke.activities.PartyActivity.onCreate(PartyActivity.java:33)
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) 
Any help?
As you have already set parent view in onCreate() function.
setContentView(R.layout.activity_party);
Will set the parent layout for the current activity.Which you have already initailzed this in onCreate() function of activity.
All these views(
photoView, dateView, directionVew,
priceView , descriptionView
) are child layouts of activity_party . You don't need to perform setContentView() on these views.
Those views will automatically display your updated data , which you have updated in following line of codes :
photoView.setImageBitmap(party.getPhoto());
dateView.setText(Session.getInstance().getDateFormat().format(party.getDate().getTime()));
directionVew.setText(party.getDirection());
priceView.setText(party.getPrice().toString());
descriptionView.setText(party.getDescripcion());
Since you called partyLayout.removeAllViews(); you removed all views from the layout. That's why findViewById() returns null.

My application in android keeps on crashing

I keep on getting this error in my logcat and don't know why. I don't seem to have any errors in code either. Any help will be greatly appreciated.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.trainyourmath, PID: 26931
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.trainyourmath/com.example.admin.trainyourmath.MainActivity}: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
at com.example.admin.trainyourmath.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5551) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
You are using your MainActivity as a OnClickListener which it does not implement properly. Add implements View.OnClickListener to the end of your MainActivity class declaration if it's not already there. Should look something like:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
You also need to implement all the methods of the View.OnClickListener so inside your MainActivity class add:
#Override
public void onClick(View v) {
// Do stuff
}

NPE error in Android Studio

I am getting the following error in Android Studio and I am unable to resolve it:
07-06 19:44:41.798 4491-4491/com.example.first_app D/AndroidRuntime: Shutting down VM
07-06 19:44:41.799 4491-4491/com.example.first_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.first_app, PID: 4491
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first_app/com.example.first_app.welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.view.View.toString()' 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.String android.view.View.toString()' on a null object reference
at com.example.first_app.welcome.onCreate(welcome.java:22)
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) 
07-06 19:44:44.696 4491-4497/com.example.first_app W/art: Suspending all threads took: 46.612ms
07-06 19:44:46.517 4491-4491/com.example.first_app I/Process: Sending signal. PID: 4491 SIG: 9
THIS IS THE ERROR I M INCURRING
CODE FOR welcome.java
public class welcome extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
TextView my_msg;
String name;
my_msg= (TextView)findViewById(R.id.textView3);
name= "Welcome " +findViewById(R.id.textView2).toString();
my_msg.setText(name);
}
}
XML CODE FOR welcome.xml ...Please refer it..it only shows the message on screen "welcome Name"..the name is entered by user through activity_form.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView3"
android:layout_gravity="center"
android:layout_weight="1" />
</LinearLayout>
First the way to solve your exception to type cast the View and getText from that View
(TextView) findViewById(R.id.textView2).getText().toString();
So the final String will look like
name= "Welcome " + (TextView) findViewById(R.id.textView2).getText().toString();
SUGGESTION
Use proper naming conventions for views, Rather than naming view1 view2 etc

TabHost Unable to add tab

I want to use with TabHost.
but its cannt successful to add tab.
when it Coming to do tabHost.addTab(tab1); it fallses. withNullPointerException
XML
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="0.2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="fill_parent"></FrameLayout>
</LinearLayout>
</TabHost>
JAVA
package abc.trempital.Activities;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TabHost;
import android.content.Intent;
import android.widget.TabHost.TabSpec;
import abc.trempital.R;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
tab1.setIndicator("first");
tab1.setContent(new Intent(this, SuggestsTabActivity.class));
tab2.setIndicator("second");
tab2.setContent(new Intent(this, WantedsTabActivity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
}
}
ERROR
06-02 13:00:22.657 15890-15890/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: abc.button, PID: 15890
java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.button/abc.trempital.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' 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 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
at android.widget.TabHost.addTab(TabHost.java:218)
at abc.trempital.Activities.MainActivity.onCreate(MainActivity.java:30)
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) 
Try this example from official sites of andorid.
TabHost is very old, please use TabLayout instead https://developer.android.com/reference/android/support/design/widget/TabLayout.html

Categories

Resources