I'm trying to populate an EditText
in my Fragment
with received text from other app.
EditText holds a value, but I'm not sure why I'm getting a NullPointerException
.
Here is my Fragment Class :
public class AddTab extends Fragment implements View.OnClickListener {
View view;
Button mButton;
EditText textBox;
String url;
private OnFragmentInteractionListener mListener;
public AddTab() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_add, container, false);
mButton = (Button)view.findViewById(R.id.button);
textBox = (EditText) view.findViewById(R.id.editText);
mButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
//some code
}
});
return view;
}
...
// some other methods
public void receiveURL(String url) {
textBox.setText(url);
//this is where I'm getting the exception. Does EditText hold the value it has got in onCreateView()??
}
}
How can I correctly update the fragment's UI here. i.e.populate my EditText with the value passed to receiveURL method?
Any help please?
Thanks!
EDIT :
StackTrace :
03-24 13:30:56.986 7424-7424/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nikhil.amazon1, PID: 7424
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nikhil.amazon1/com.example.nikhil.amazon1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.example.nikhil.amazon1.AddTab.receiveURL(AddTab.java:122)
at com.example.nikhil.amazon1.MainActivity.handleSendText(MainActivity.java:102)
at com.example.nikhil.amazon1.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
MainActivity class : From this class I'm calling receiveURL() method.
public class MainActivity extends FragmentActivity implements ListTab.OnFragmentInteractionListener, AddTab.OnFragmentInteractionListener{
PendingIntent pendingIntent;
MyPagerAdapter objAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
objAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(objAdapter);
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager.setCurrentItem(1);
tabs.setShouldExpand(true);
tabs.setViewPager(pager);
//code for listening to intent from browser or other apps
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
Fragment f = objAdapter.getItem(1);
AddTab t = (AddTab) f;
t.receiveURL(sharedText);
//from here I'm calling
}
}
}
I'm using pagerslidingtabstrip
to create tabs. I obtained the adapter's reference and got the instance of my fragment AddTab
. Then called my method receiveURL().
All this happens only when user shares a URL (text) from other app.