I am making a simple app to learn using fragment. I want to transfer an string from one activity to a fragment. I did it by Bundle by it does not work. In the fragment class it doesn not accept the code and highlight it as unreachable code. Here is my code: Main Activity: public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
//
setContentView(R.layout.activity_main);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
//List view
final ListView list = (ListView)findViewById(R.id.questionsList);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, R.layout.list_item, adobe_products);
//final TextView text = (TextView)findViewById(R.id.textView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String selectedFromList =(String) (list.getItemAtPosition(position));
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("key", selectedFromList);
fragment.setArguments(bundle);
}
});
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Main Activity XML
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<ListView
android:id="@+id/questionsList"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_weight="0.07" >
</ListView>
<fragment
android:id="@+id/article_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.example.layout.MyFragment" />
</LinearLayout>
Fragment:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.activity_fragment, container, false);
Bundle bundle = getArguments();
String selected = bundle.getString("key");
TextView text = (TextView) myFragmentView.findViewById(R.id.text);
text.setText(selected);
// Inflate the layout for this fragment
return myFragmentView;
}
}
UPDATE: I just made the changes, I don't have this error anymore, but it still does not open the app. It stops in loading. Here is the catLog:
02-22 10:45:05.791: E/AndroidRuntime(1867): FATAL EXCEPTION: main
02-22 10:45:05.791: E/AndroidRuntime(1867): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layout/com.example.layout.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class fragment
02-22 10:45:05.791: E/AndroidRuntime(1867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-22 10:45:05.791: E/AndroidRuntime(1867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-22 10:45:05.791: E/AndroidRuntime(1867): at android.app.ActivityThread.access$600(ActivityThread.java:141)