![](/img/trans.png)
[英]My code shows error inflating class fragment, for recycler view. This is my code, Adapter class, and all the xml files I used
[英]I want to apply an onclicklistener to a textview inside a cardview WITHOUT RECYCLER VIEW. My code is as follows
xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp"
android:background="@color/back"
>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Central Library"
android:textSize="18dp"
android:id="@+id/lib"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dispensary"
android:textSize="18dp"
android:id="@+id/dis"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Auditorium"
android:textSize="18dp"
android:id="@+id/audi"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bank and ATM"
android:textSize="18dp"
android:id="@+id/atm"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="VIP Guest House"
android:textSize="18dp"
android:id="@+id/gh"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
java代码..
package com.example.hp.manit;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
//code starts from here
public class Facilities extends AppCompatActivity implements View.OnClickListener{
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facilities);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView t1=(TextView)findViewById(R.id.lib);
t1.setOnClickListener(this);
TextView t2=(TextView)findViewById(R.id.dis);
t2.setOnClickListener(this);
TextView t3=(TextView)findViewById(R.id.audi);
t3.setOnClickListener(this);
TextView t4=(TextView)findViewById(R.id.atm);
t4.setOnClickListener(this);
TextView t5=(TextView)findViewById(R.id.gh);
t5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.lib:
// do your code
intent = new Intent(Facilities.this, Library.class);
startActivity(intent);
break;
case R.id.dis:
// do your code
intent = new Intent(Facilities.this, Library.class);
startActivity(intent);
break;
case R.id.audi:
// do your code
intent = new Intent(Facilities.this, Auditorium.class);
startActivity(intent);
break;
case R.id.atm:
// do your code
intent = new Intent(Facilities.this, BankATM.class);
startActivity(intent);
break;
case R.id.gh:
// do your code
intent = new Intent(Facilities.this, GuestHouse.class);
startActivity(intent);
break;
}
}
}
注意:此布局文件 factory.xml 在选项卡布局内的片段中膨胀。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = getArguments();
int tabPositon = args.getInt(TAB_POSITION);
if(tabPositon==0) {
View v0 = inflater.inflate(R.layout.activity_academics, container, false);
return v0;
}
else if(tabPositon==1) {
View v1 = inflater.inflate(R.layout.activity_departments, container, false);
return v1;
}
else if(tabPositon==2) {
View v2 = inflater.inflate(R.layout.activity_facilities, container, false);
return v2;
}
else {
View v3 = inflater.inflate(R.layout.activity_students_zone, container, false);
return v3;
}
}
我已经对每个文本视图应用了一个监听器,以便使用意图打开一个新的活动。 但它不起作用
我只想在单击 cardview 时打开一个新活动 Plz 帮助如何 ?????
只需在 XML 中的所有 Textview 中添加以下属性并尝试
<android:clickable="true">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.