E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.liliyu.easyresumebuilder, PID: 11145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liliyu.easyresumebuilder/com.example.liliyu.easyresumebuilder.MainActivity}: android.view.InflateException: Binary XML file line #10: RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView{df7f7c9 VFED..... ......I. 0,0-0,0 #7f070061 app:id/main_activity1}, adapter:null, layout:null, context:com.example.liliyu.easyresumebuilder.MainActivity@2857d33
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
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:id="@+id/main_activity1"
tools:context="com.example.liliyu.easyresumebuilder.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_medium"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/user_name_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_small"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/user_picture"
android:gravity="center_vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/caption_text_size"
tools:text="Your name" />
</LinearLayout>
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/user_name_layout"
android:layout_toLeftOf="@+id/user_picture"
tools:text="Your email" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.RecyclerView>
MainActivity.java
@SuppressWarnings("ConstantConditions")
public class MainActivity extends AppCompatActivity {
private BasicInfo basicInfo;
private RecyclerView recyclerView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadData();
setupUI();
}
private void setupUI() {
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.main_activity1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
setupBasicInfo();
}
private void setupBasicInfo() {
((TextView) findViewById(R.id.name)).setText(TextUtils.isEmpty(basicInfo.name)
? "Your name"
: basicInfo.name);
((TextView) findViewById(R.id.email)).setText(TextUtils.isEmpty(basicInfo.email)
? "Your email"
: basicInfo.email);
ImageView userPicture = (ImageView) findViewById(R.id.user_picture);
if (basicInfo.imageUri != null) {
ImageUtils.loadImage(this, basicInfo.imageUri, userPicture);
} else {
userPicture.setImageResource(R.drawable.user_ghost);
}
findViewById(R.id.edit_basic_info).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, BasicInfoEditActivity.class);
intent.putExtra(BasicInfoEditActivity.KEY_BASIC_INFO, basicInfo);
startActivityForResult(intent, REQ_CODE_EDIT_BASIC_INFO);
}
});
}
private void loadData() {
connectAndGetApiData();
basicInfo = basicInfo == null ? new BasicInfo() : basicInfo;
}
public void connectAndGetApiData() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
ResumeService resumeService=retrofit.create(ResumeService.class);
Call<Resume> call =resumeService.getResume();
try {
call.enqueue(new Callback<Resume>() {
@Override
public void onResponse(Call<Resume> call, Response<Resume> response) {
recyclerView.setAdapter(new ResumeAdapter(getApplicationContext(),response.body()));
}
@Override
public void onFailure(Call<Resume> call, Throwable throwable) {
System.out.println(throwable.toString());
}
});
}catch(Throwable e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您不能将孩子放在XML的RecyclerView
中。看来您目前在ViewHolder
中拥有的东西。那不是怎么做。
制作一个新的布局XML文件,并将RecyclerView
中包装的内容移动到该文件中。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
//delete all elements
</android.support.v7.widget.RecyclerView
>
答案 1 :(得分:1)
在您的RecyclerView中添加一个parentLayout(可以是LinearLayout),并从RecyclerView中删除子级。