I am trying to implement a simple communication between 2 Fragments in an Activity. The first fragment has a button that when pressed is supposed to send the current time in milliseconds to the second fragment. The second fragment displays this at a Textview. Well I just don't what I am doing wrong. Java and XML files are the following
package com.example.android.rssreader;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.TextView;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
}
@Override
public void onRssItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
fragment.setText(link);
}
}
activity_rssfeed.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.android.rssreader.MyListFragment"
tools:layout="@layout/fragment_rsslist_overview">
</fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssreader.DetailFragment"
tools:layout="@layout/fragment_rssitem_detail">
</fragment>
</LinearLayout>
DetailFragment
package com.example.android.rssreader;
/**
* Created on 9/19/2016.
*/
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
public static final String EXTRA_URL ="url";
View view;
TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
txt = (TextView) view.findViewById(R.id.detailsText);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
String link = bundle.getString("url");
setText(link);
}
}
public void setText(String url) {
txt.setText(url);
}
}
fragment_rssitem_detail
<?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:orientation="vertical" >
<TextView
android:id="@+id/detailsText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dip"
android:text="@string/default_text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30dip" />
</LinearLayout>
MyListFragment
package com.example.android.rssreader;
/**
* Created on 9/19/2016.
*/
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
private OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview,
container, false);
Button button = (Button) view.findViewById(R.id.updateButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail("fake");
}
});
return view;
}
public interface OnItemSelectedListener {
void onRssItemSelected(String link);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) context;
} else {
throw new ClassCastException(context.toString()
+ " must implement MyListFragment.OnItemSelectedListener");
}
}
// triggers update of the details fragment
public void updateDetail(String uri) {
// create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// send data to activity
listener.onRssItemSelected(newTime);
}
}
fragment_rsslist_overview
<?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:orientation="vertical" >
<Button
android:id="@+id/updateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to update"
/>
</LinearLayout>
Thanks in advance for in suggestion!
The Stack Trace
09/21 21:59:32: Launching app
$ adb push E:\AndroidProjects\VogellaTut\RSSReader\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.android.rssreader
$ adb shell pm install -r "/data/local/tmp/com.example.android.rssreader"
pkg: /data/local/tmp/com.example.android.rssreader
Success
$ adb shell am start -n "com.example.android.rssreader/com.example.android.rssreader.RssfeedActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.android.rssreader.test | com.example.android.rssreader
Waiting for application to come online: com.example.android.rssreader.test | com.example.android.rssreader
Waiting for application to come online: com.example.android.rssreader.test | com.example.android.rssreader
Connecting to com.example.android.rssreader
E/Trace: error opening trace file: No such file or directory (2)
I/System.out: Sending WAIT chunk
W/ActivityThread: Application com.example.android.rssreader is waiting for the debugger on port 8100...
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8605', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1453)
V/ActivityThread: Class path: /data/app/com.example.android.rssreader-1.apk, JNI path: /data/data/com.example.android.rssreader/lib
W/dalvikvm: VFY: unable to resolve virtual method 258: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
W/dalvikvm: VFY: unable to resolve virtual method 261: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
I/InstantRun: Instant Run Runtime started. Android package is com.example.android.rssreader, real application class is null.
W/InstantRun: No instant run dex files added to classpath
E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
W/dalvikvm: VFY: unable to resolve check-cast 1885 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
W/dalvikvm: VFY: unable to resolve const-class 1885 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
W/dalvikvm: VFY: unable to resolve virtual method 310: Landroid/app/Fragment;.onAttach (Landroid/content/Context;)V
I/Adreno200-EGL: <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: (CL3050818)
Build Date: 01/18/13 Fri
Local Branch: lge_changes
Remote Branch:
Local Patches:
Reconstruct Branch:
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d8d438)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.android.rssreader.MyListFragment.updateDetail(MyListFragment.java:53)
at com.example.android.rssreader.MyListFragment$1.onClick(MyListFragment.java:27)
at android.view.View.performClick(View.java:4101)
at android.view.View$PerformClick.run(View.java:17082)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4940)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)
Disconnected from the target VM, address: 'localhost:8605', transport: 'socket'