I am making an app in which in I am using fragments. I have made it in portrait as well as in landscape mode.
In portrait mode what i have done is that a ListView will be shown and on selecting an item on next activity the description will be shown
In landscape mode I have used two fragments in same activity in which one fragment has ListView and second fragment has description
but when i am clicking the item in list view it is showing me error that
cannot invoke method on null TextView object
but I have initialised it n onActivityCreated() method in Fragment2.java then why is it giving this error?
Main.xml (portrait)
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.grepthor.fragment4.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment1"
android:name="com.grepthor.fragment4.Fragment1"
/>
</LinearLayout>
Main.xml (landscape)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/fragment1"
android:name="com.grepthor.fragment4.Fragment1"/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/fragment2"
android:name="com.grepthor.fragment4.Fragment2"/>
</LinearLayout>
Main.java
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements Fragment1.Communicator {
Fragment1 f1;
Fragment2 f2;
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm=getFragmentManager();
f1= (Fragment1) fm.findFragmentById(R.id.fragment1);
f1.setCommunicator(this);
}
@Override
public void respond(int position) {
f2= (Fragment2) fm.findFragmentById(R.id.fragment2);
if(f2!=null && f2.isVisible())
{
f2.changeData(position);
}
else
{
Intent i=new Intent(this,SecondActivity.class);
i.putExtra("index",position);
startActivity(i);
}
}
}
Fragment1.xml
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.grepthor.fragment4.Fragment1">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</RelativeLayout>
Fragment1.java
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Fragment1 extends Fragment implements AdapterView.OnItemClickListener {
ListView l;
Communicator comm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_fragment1,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
l= (ListView) getActivity().findViewById(R.id.list);
ArrayAdapter ad=ArrayAdapter.createFromResource(getActivity(),R.array.topic,android.R.layout.simple_list_item_1);
l.setAdapter(ad);
// Toast.makeText(getActivity(),"helo",Toast.LENGTH_LONG).show();
l.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
comm.respond(position);
}
public void setCommunicator(Communicator c)
{
comm=c;
}
interface Communicator
{
void respond(int data);
}
}
Fragment2.xml
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.grepthor.fragment4.Fragment2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"/>
</RelativeLayout>
Fragment2.java
public class Fragment2 extends Fragment {
TextView t;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_fragment2,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Toast.makeText(getActivity(),"helo",Toast.LENGTH_LONG).show();
t=(TextView)getActivity().findViewById(R.id.text);
}
public void changeData(int position)
{
String des[]= getResources().getStringArray(R.array.des);
t.setText(des[position]);
}
}
SecondActivity.xml
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.grepthor.fragment4.SecondActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment2"
android:name="com.grepthor.fragment4.Fragment2"/>
</LinearLayout>
SecondActivity.java
import android.content.Intent;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i=getIntent();
int position=i.getExtras().getInt("index",0);
FragmentManager fm=getFragmentManager();
Fragment2 f2= (Fragment2) fm.findFragmentById(R.id.fragment2);
f2.changeData(position);
}
}
Strings.xml
<resources>
<string name="app_name">Fragment4</string>
<string-array name="topic">
<item>Heading</item>
<item>Stories</item>
<item>Settings</item>
<item>Music</item>
</string-array>
<string-array name="des">
<item>ldwkflkwdflkdqdvmqmdvmqdvmqlkrvqkneroqknre;qoiengqrknqrqvqfvaa</item>
<item>vqdlkvnqldvnqdnvqdnvqepojpqojjoergjelgefbfmsmfv</item>
<item>dwkfmnqdkwnfqkjwdnflwndfqkwheflqierqieurpqeirqrkmqwrgfqnqnmdqd</item>
<item>skjsbdkafsknaslnaslfnaljfhadfkjgjhgasjdvsadablvasfgasfkgnjlkrglrgqmqbvmadbsv</item>
</string-array>
</resources>
Logcat
FATAL EXCEPTION: main
Process: com.grepthor.fragment4, PID: 31014
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grepthor.fragment4/com.grepthor.fragment4.SecondActivity}: 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:2430)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5437)
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 com.grepthor.fragment4.Fragment2.changeData(Fragment2.java:35)
at com.grepthor.fragment4.SecondActivity.onCreate(SecondActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6532)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2383)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5437)
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)