我正在尝试访问我的片段,因此我可以调用其中的方法。请提出任何建议吗?
这就是我在mainActivity中的onCreate中所做的:
mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.current_fragment);
这是我的片段布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/current_fragment"
android:background="@drawable/bg_gradient">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconImageView"
android:src="@mipmap/clear_night"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="@string/weatherIconDesc"
android:layout_above="@+id/locationLabel"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="requesting current location..."
android:id="@+id/locationLabel"
android:textColor="@android:color/white"
android:textSize="22sp"
android:layout_above="@+id/timeLabel"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="--"
android:id="@+id/temperatureLabel"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white"
android:textSize="125sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degreeImageView"
android:layout_alignTop="@+id/temperatureLabel"
android:layout_toRightOf="@+id/temperatureLabel"
android:layout_toEndOf="@+id/temperatureLabel"
android:src="@mipmap/degree"
android:layout_marginTop="27dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..."
android:id="@+id/timeLabel"
android:textStyle="bold"
android:layout_above="@+id/degreeImageView"
android:layout_centerHorizontal="true"
android:textColor="#80ffffff"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/temperatureLabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="@+id/linearLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HUMIDITY"
android:id="@+id/humidityLabel"
android:textColor="#80ffffff"
android:gravity="center_horizontal"
android:textIsSelectable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="--"
android:id="@+id/humidityValue"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WIND SPEED"
android:id="@+id/windSpeedLabel"
android:textColor="#80ffffff"
android:gravity="center_horizontal"
android:textIsSelectable="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="--"
android:id="@+id/windSpeedValue"
android:textSize="24sp"
android:textColor="@android:color/white"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="KM/h"
android:id="@+id/kmh"
android:textColor="#80ffffff"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RAIN?"
android:id="@+id/precipLabel"
android:textColor="#80ffffff"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="--"
android:id="@+id/precipValue"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Getting current weather..."
android:id="@+id/summaryLabel"
android:textColor="@android:color/white"
android:textSize="18sp"
android:layout_marginTop="15dp"
android:gravity="fill_horizontal"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom= "10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/refreshImageView"
android:src="@mipmap/refresh"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_alignLeft="@+id/progressBar"
android:layout_alignStart="@+id/progressBar"
android:layout_alignTop="@+id/progressBar"
android:layout_alignBottom="@+id/progressBar"
android:layout_alignRight="@+id/progressBar"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
这是我的主要活动:
<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"
tools:context=".MainActivity">
<koemdzhiev.com.stormy.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
这是我的片段类:
public class Current_forecast_fragment extends Fragment {
private MainActivity mActivity;
@InjectView(R.id.timeLabel)
TextView mTimeLabel;
@InjectView(R.id.temperatureLabel)
TextView mTemperatureLabel;
@InjectView(R.id.humidityValue)
TextView mHumidityValue;
@InjectView(R.id.precipValue)
TextView mPrecipValue;
@InjectView(R.id.summaryLabel)
TextView mSummaryLabel;
@InjectView(R.id.locationLabel)
TextView mLocationLabel;
@InjectView(R.id.windSpeedValue)
TextView mWindSpeedValue;
@InjectView(R.id.iconImageView)
ImageView mIconImageView;
@InjectView(R.id.refreshImageView)
ImageView mRefreshImaveView;
@InjectView(R.id.progressBar)
ProgressBar mProgressBar;
@InjectView(R.id.degreeImageView)
ImageView mDegreeImageView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = ((MainActivity) getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.current_forefast_fragment, container, false);
ButterKnife.inject(this, v);
return v;
}
public void toggleRefresh() {
if (mProgressBar.getVisibility() == View.INVISIBLE) {
mProgressBar.setVisibility(View.VISIBLE);
mRefreshImaveView.setVisibility(View.INVISIBLE);
} else {
mProgressBar.setVisibility(View.INVISIBLE);
mRefreshImaveView.setVisibility(View.VISIBLE);
}
// updates the dysplay with the data in the CUrrentWeather locaal object
}
public void updateDisplay() {
Current current = mActivity.mForecast.getCurrent();
//setting the current weather details to the ui
mTemperatureLabel.setText(current.getTemperature() + "");
mTimeLabel.setText("At " + current.getFormattedTime() + " it will be");
mHumidityValue.setText(current.getHumidity() + "%");
mPrecipValue.setText(current.getPrecipChange() + "%");
mSummaryLabel.setText(current.getSummery());
mWindSpeedValue.setText(current.getWindSpeed() + "");
mLocationLabel.setText(current.getTimeZone());
mActivity.getLocationName();
Drawable drawable = ContextCompat.getDrawable(mActivity, current.getIconId());
mIconImageView.setImageDrawable(drawable);
}
}
这是它崩溃的地方:
public class MainActivity extends AppCompatActivity {
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Current","Hourly","Daily"};
int Numboftabs =3;
Current_forecast_fragment mCurrent_forecast_fragment;
public static final String TAG = MainActivity.class.getSimpleName();
public static final String LOCATION_KEY = "location_key";
public Forecast mForecast;
public static final String DAILY_FORECAST = "DAILY_FORECAST";
public static final String HOURLY_FORECAST = "HOURLY_FORECAST";
//default coordinates - Gotse Delchev, UK Lati:57.156866 ; Long:
private double latitude = 41.5667;
private double longitude = 23.7333;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------MY CODE STARTS HERE-----------------
mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.current_fragment);
Log.d("MainActivity",mCurrent_forecast_fragment.getTag() + "Georgi");
}
答案 0 :(得分:2)
current_fragment是你的片段RelativeLayout的id,而不是你片段的id
CREATE FUNCTION "public"."log_test_trigger" () RETURNS "trigger" AS 'BEGIN
...
IF (TG_OP = ''UPDATE'') THEN
IF (NEW.name <> OLD.name) THEN
INSERT INTO log_test (type, table, field, old_val, new_val, user, event) VALUES (NEW.type, TG_RELNAME, ''name'', OLD.name, NEW.name, NEW.user, ''UPDATE'');
END IF;
...
END;' LANGUAGE "plpgsql"
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
将返回null。如果你想通过id找到片段,请将它放在你的活动布局中,如下所示:
mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.current_fragment);
希望这有帮助!
答案 1 :(得分:2)
看起来您当前的xml代码中没有片段。 您需要在RelativeLayout中添加一个片段。 您可以在Android Developer的网站上看到完整的教程:http://developer.android.com/training/basics/fragments/creating.html
答案 2 :(得分:1)
我将尝试改进ResolutioN帖子。问题可能是xml内容,这应该很容易解决。 样本布局:
<fragment
android:name="Current_forecast_fragment"
android:id="@+id/current_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
注意:您需要插入android:name
,以便将其类型转换为对象。
编辑: 在MainActivity中,它需要导入正确的类。 示例代码:
import package.Current_forecast_fragment;
注意:您需要通过指定正确的路径来引用片段类。
同样,这不应该太困难。玩得开心,祝你好运,不要放弃!