无法启动活动无法在Recyclerview中启动活动ComponentInfo

时间:2017-03-14 19:06:07

标签: android android-studio

我正在尝试使用改进来解析json对象解析但是它给了我错误无法在运行时启动活动ComponentInfo ...

模型类

public class DailyWeatherResponse {

@SerializedName("city")
@Expose
private City city;
@SerializedName("cod")
@Expose
private Integer cod;
@SerializedName("message")
@Expose
private Double message;
@SerializedName("cnt")
@Expose
private Integer cnt;
@SerializedName("list")
@Expose
private List<DailyList> list = null;

public City getCity() {
    return city;
}

public void setCity(City city) {
    this.city = city;
}

public Integer getCod() {
    return cod;
}

public void setCod(Integer cod) {
    this.cod = cod;
}

public Double getMessage() {
    return message;
}

public void setMessage(Double message) {
    this.message = message;
}

public Integer getCnt() {
    return cnt;
}

public void setCnt(Integer cnt) {
    this.cnt = cnt;
}

public List<DailyList> getList() {
    return list;
}

public void setList(List<DailyList> list) {
    this.list = list;
}

}

api接口

@GET("forecast/daily")
Call<DailyWeatherResponse> getDailyWeather(
     @Query("q") String city,
     @Query("units") String uints,
     @Query("cnt") String cnt,
     @Query("APPID") String apiKey);
片段中的

recyclerview

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

    ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
    Call<DailyWeatherResponse> call = apiInterface.getDailyWeather("London","7","metric",getResources().getString(R.string.api_key));

    call.enqueue(new Callback<DailyWeatherResponse>() {
        @Override
        public void onResponse(Call<DailyWeatherResponse> call, Response<DailyWeatherResponse> response) {
            List<DailyList> dailyLists = response.body().getList();
            recyclerView.setAdapter(new DailyWeatherAdapter(dailyLists,R.layout.fragment_daily_weather,getActivity().getApplicationContext()));
        }

        @Override
 public void onFailure(Call<DailyWeatherResponse> call, Throwable t) {
            t.printStackTrace();

        }
    });

}

片段

的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/week"
    android:text="Today"
    android:textSize="16sp"
    android:textStyle="bold"
    android:layout_weight="1"/>
<ImageView
    android:id="@+id/dailyweatherimage"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/weather"
    android:layout_weight="1"

    />
<TextView
    android:id="@+id/dailyweatherDesc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rain"
    android:textSize="16sp"
    android:textStyle="bold"
    android:layout_weight="1"/>
<TextView
    android:id="@+id/tempDay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="23"
    android:textSize="16sp"
    android:textStyle="bold"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="/"/>
<TextView
    android:id="@+id/tempNight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="25"
    android:textSize="16sp"
    android:textStyle="bold"
    />
  </LinearLayout>

recyclerview片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"
    android:scrollbars="vertical"
    android:clipToPadding="false"

    />

这是logcat中的错误

03-15 01:27:46.646 20132-20132/com.example.prem.weatherapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.prem.weatherapplication, PID: 20132
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prem.weatherapplication/com.example.prem.weatherapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                                     at com.example.prem.weatherapplication.fragment.DailyWeatherFragment.onViewCreated(DailyWeatherFragment.java:51)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                                                                                     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                                                                                     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                                                     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
                                                                                     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                                     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                                     at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                                     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1260)
                                                                                     at android.app.Activity.performStart(Activity.java:6261)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5443) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

1 个答案:

答案 0 :(得分:0)

你应该得到片段上下文而不是活动上下文,试试这个。

new LinearLayoutManager(getContext())

相关问题