我有一个MainActivity,它基本上有一个framelayout,根据用户的行为,它有不同的片段。
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#27b"
android:layout_weight=".04">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main_framelayout">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".9"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1B5F96"
android:layout_weight=".9"
android:id="@+id/activity_main_status_title"
android:text="@string/activity_main_status_title"
tools:ignore="NestedWeights" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AD3333"
android:layout_weight=".15"
android:id="@+id/activity_main_status_value"
android:text="@string/activity_main_status_value"/>
</LinearLayout>
</LinearLayout>
问题始于接口方法void WillYouAccept(final String caller)
@Override
public void WillYouAccept(final String caller) {
Connection f = new Connection();
ShiftView(f);
runOnUiThread(new Runnable() {
@Override
public void run() {
Connection b = (Connection) getFragmentManager().findFragmentById(R.layout.connection_display);
b.updateInitiator(caller);
}
});
}
行b.updateInitiator(caller);
是启动空对象引用错误的原因。 ShiftView
基本上已经将片段交换到了framelayout中,并且到目前为止效果还不错。
@Override
public void ShiftView(Object obj) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.activity_main_framelayout, (Fragment) obj);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
基本上,connection_display.xml(Connection.java)中的textview需要从MainActivity更新,并且当与MainActivity进行交互时,某种方式initiator = (TextView) view.findViewById(R.id.textView_connection_display_initiator_ID);
为null。
Connection.java
package "";
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Connection extends Fragment {
MiddleMan mCallBack;
Button accept;
Button deny;
TextView initiator;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallBack = (MiddleMan) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement ReqestConnect");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.connection_display, container, false);
mCallBack.DisplayHome();
initiator = (TextView) view.findViewById(R.id.textView_connection_display_initiator_ID);
accept = (Button) view.findViewById(R.id.button_connection_display_ACCEPT);
accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonAccept();
}
});
deny = (Button) view.findViewById(R.id.button_connection_display_DENY);
deny.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonDeny();
}
});
return view;
}
private void buttonAccept() {
System.out.println("Accept Button Pressed");
}
private void buttonDeny() {
System.out.println("Deny Button Pressed");
}
public void updateInitiator(final String s) {
initiator.setText(s);
}
}
connection_display.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="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/textView_connection_display_title"
android:id="@+id/textView_connection_display_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_marginTop="45dp"
android:layout_weight=".004">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/textView_connection_display_ID_label"
android:id="@+id/textView_connection_display_ID_label" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView_connection_display_initiator_ID"
android:text="@string/textView_connection_display_initiator_ID" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight=".004">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/textView_connection_display_exp"
android:id="@+id/textView_connection_display_exp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight=".04">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="@string/button_connection_display_ACCEPT"
android:id="@+id/button_connection_display_ACCEPT"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_connection_display_DENY"
android:id="@+id/button_connection_display_DENY"
tools:ignore="ButtonStyle" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
b肯定会变为null,但上面的位总是找到片段,所以我用这个解决了它:
@Override
public void WillYouAccept(final String caller) {
final Connection f = new Connection();
ShiftView(f);
runOnUiThread(new Runnable() {
@Override
public void run() {
f.updateInitiator(caller);
}
});
}