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
Related
I'm simply trying to run a method within a fragment from my main activity. getFragments() lists all the fragments in my application correctly. However when calling findFragmentByTag() the result is always null! I've looked around and no solution seems to help.
Here's where I call my fragment manager
fm.executePendingTransactions();
Log.d("Fragmento", fm.getFragments().toString());
Log.d("Fragmento", fm.findFragmentByTag("frag3").toString());
where fm is
`FragmentManager fm = getSupportFragmentManager();`
My fragment Code is
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment3 extends Fragment {
TextView status;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment3, container, false);
Log.d("InFragment", this.getClass().toString());
return v;
}
public static Fragment3 newInstance() {
Fragment3 f = new Fragment3();
return f;
}
public void setStatus(String msg){
status = (TextView)getView().findViewById(R.id.canLog);
status.setText(status.getText()+"\n"+msg);
}
}
My layout is very simple, and i've defined an ID and a tag within the layout for the fragment.
`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="frag3"
android:id="#+id/fragment3">
</LinearLayout>`
finally my error log
05-25 16:56:54.949 13263-13263/com.archronix.infotainment E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.archronix.infotainment, PID: 13263
java.lang.IllegalStateException: Could not execute method for
android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4458)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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.support.v4.app.Fragment.toString()' on a null
object reference
at
com.archronix.infotainment.MainActivity.buttonClicked(MainActivity.java:445)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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)
This is probably a very stupid question however fragments seem to baffle me completely.
Problem:
Setting the tag in the LinearLayout doesn't work, the Fragmentmangager wont find it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="frag3" <-- not good
android:id="#+id/fragment3">
`
Solution:
If your define the fragment in the xml:
<fragment android:name="com.example.YourFragment"
android:id="#+id/frag3Id"
android:layout_width="match_parent"
android:tag="frag3"
android:layout_height="match_parent" />
you can find it with:
fm.findFragmentByTag("frag3")
or
fm.findFragmentById(R.id.frag3Id)
Or you can set the tag withe the add and replace functions of the FragmentTransaction
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.
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);