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.
Related
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.
when i start my app with out network and call my data in my database app crash and this is my code
public void getAllMovies() {
findData = realm.where(MovieDb.class).findAll();
movieArrayList = new ArrayList(findData);
movieMethod();
}
and i make my conflagration in here
public class myApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
RealmConfiguration configuration = new RealmConfiguration
.Builder(this).name("Movie_database.realm").build();
Realm.setDefaultConfiguration(configuration);}}
and moviedb is my realm object
my app crash in line
findData = realm.where(MovieDb.class).findAll();
and this is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.massive.movieapp, PID: 4222
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.massive.movieapp/com.massive.movieapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' 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 'io.realm.RealmQuery io.realm.Realm.where(java.lang.Class)' on a null object reference
at com.massive.movieapp.FragmentForActivity.getAllMovies(FragmentForActivity.java:143)
at com.massive.movieapp.FragmentForActivity.CallNetwork(FragmentForActivity.java:62)
at com.massive.movieapp.FragmentForActivity.onCreate(FragmentForActivity.java:55)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
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
The exception happens because your realm variable is null when you call the getAllMovies method.
Remember to call realm = Realm.getDefaultInstance(); previously.
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
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
I want to put a URL received from Application B in to my application, in a EditText.
But im receiving null
Here is my code where im trying to get the intent:
Intent intent = getIntent();
mStreamUrl = intent.getDataString();
FinalurlPrimit.setText(mStreamUrl);
layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_torrent_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_below="#+id/include"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp">
<EditText
android:id="#+id/bTorrentUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:hint="#string/Text_magnet_sau_torrent_url"
android:text="#+id/FinalurlPrimit"/>
</android.support.design.widget.TextInputLayout>
How can achieve this?
Thank you
edit
Log:
02-28 16:54:09.900 5698-5698/ro.vrt.videoplayerstreaming E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.vrt.videoplayerstreaming, PID: 5698
java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.vrt.videoplayerstreaming/ro.vrt.videoplayerstreaming.TorrentPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' 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.TextView.setText(java.lang.CharSequence)' on a null object reference
at ro.vrt.videoplayerstreaming.TorrentPlayer.onCreate(TorrentPlayer.java:48)
at android.app.Activity.performCreate(Activity.java:6251)
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)
02-28 16:54:11.988 5698-5698/ro.vrt.videoplayerstreaming I/Process: Sending signal. PID: 5698 SIG: 9
If you delete android:text="#+id/FinalurlPrimit". everything work fine.
But I want the URL received, to appear in EditText
I do not have access to application B, and information from application B receive correct. Just can not display in EditText
FirstActivity
String yourURL ="http://stackoverflow.com/";
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("URL",yourURL);
startActivity(i);
SecondActivity
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("URL");
FinalurlPrimit.setText(value);
}
Please remove android:text="#+id/FinalurlPrimit". I don't think you need it. Between, android: text used to display the text you want it to show on EditText when there are no input on it. No id is required.
In OnCreate method, make sure you initialize the EditText as below
FinalurlPrimit= (EditText)findViewById(R.id.bTorrentUrl);