Android 访问活动中的片段时执行空指针执行
我试图访问我的片段,以便调用其中的方法。有什么建议吗 这是我在主要活动的onCreate中所做的:Android 访问活动中的片段时执行空指针执行,android,android-fragments,nullpointerexception,Android,Android Fragments,Nullpointerexception,我试图访问我的片段,以便调用其中的方法。有什么建议吗 这是我在主要活动的onCreate中所做的: mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.current_fragment); 这是我的片段布局: <RelativeLayout xmlns:android="http://schemas.android.com/ap
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>
这就是它崩溃的地方:
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");
}
当前_片段是片段相对的id,而不是片段的id,所以
mCurrent_forecast_fragment = (Current_forecast_fragment) getSupportFragmentManager().findFragmentById(R.id.current_fragment);
将返回null。如果要按id查找片段,请将其放置在活动布局中,如下所示:
<RelativeLayout 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:background="@drawable/bg_gradient"
android:orientation="vertical" >
<fragment
android:id="@+id/current_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
希望这有帮助 看起来您当前的xml代码中没有片段。 您需要在RelativeLayout中添加一个片段。
您可以在Android开发者网站上看到完整的教程:我将尝试改进分辨率帖子。问题很可能是xml内容,这应该很容易解决。 布局示例:
<fragment
android:name="Current_forecast_fragment"
android:id="@+id/current_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
注意:您需要通过指定正确的路径来引用片段类
同样,这不应该太难。玩得开心,祝你好运,不要放弃 出于某种原因,这不起作用。它再次导致NullPointerException:/Yes。这就是我得到的结果:原因:java.lang.NullPointerException:试图在koemdzhiev.com.stormy.Current_forecast_fragment.getTag()上的空对象引用上调用虚拟方法“java.lang.String koemdzhiev.com.stormy.MainActivity.onCreate(MainActivity.java:66)在android.app.Activity.performCreate(Activity.java:6251)上,这意味着您当前的\u forecast\u片段为空,您可以发布您的活动代码和xml布局吗?解决方案我刚刚意识到我在此处将错误的conde作为我的主要活动:/will change itDone。很明显。我试图从我的主要活动中获取片段,这是错误的,获取null是有意义的。你知道我如何访问我的主要活动中的片段,所以我可以调用它的方法吗?谢谢你的建议。我确实尝试过这个,但我得到了相同的空指针验证:/@GeorgiKoemdzhiev,我在我的帖子(编辑下)中添加了新的注释,因为您向MainActivity添加了更多代码。但在本例中,我没有看到导入。
<RelativeLayout 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:background="@drawable/bg_gradient"
android:orientation="vertical" >
<fragment
android:id="@+id/current_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<fragment
android:name="Current_forecast_fragment"
android:id="@+id/current_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
import package.Current_forecast_fragment;