-1
我有一個MainActivity,它基本上有一個framelayout,它有不同的片段坐在它取決於用戶在做什麼。android null對象引用,儘管存在
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一個TextView(Connection.java)需要從在MainActivity更新,並以某種方式initiator = (TextView) view.findViewById(R.id.textView_connection_display_initiator_ID);
爲空時,它被與來自MainActivity交互。
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>
來自'findViewById()'調用的NPE通常是請求的視圖不存在的結果,因爲您正在搜索的視圖的子視圖不存在。請確保您嘗試獲取的視圖的視圖已添加到父級,如果它在一個片段中,請改用'fragment.findViewById()'。 – ygesher