我正在制作一个应用程序,其中我正在使用片段。我以纵向和横向模式制作了它。
在纵向模式下,我所做的是将显示一个ListView,在选择下一个活动的项目时,将显示描述
在横向模式下,我在同一活动中使用了两个片段,其中一个片段具有ListView,第二个片段具有描述
但是当我点击列表视图中的项目时,它显示错误
cannot invoke method on null TextView object
但是我已经在Fragment2.java中初始化了onActivityCreated()方法,那为什么会出现这个错误呢?
Main.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.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(格局)
<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)
答案 0 :(得分:2)
您的Fragment2
拥有TextView
,并且您正试图在Activity
中找到它。您需要在Fragment2
中执行此操作,并将其从onActivityCreated()
:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_fragment2,container,false);
TextView textView = (TextView) rootView.findViewById(R.id.text);
}
同样在将来,只发布代码的相关部分与发布所有部分。
答案 1 :(得分:0)
将textView初始化放到onViewCreated:
//Fragment2.java
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
t = (TextView) view.findViewById(R.id.text);
}