Java 在Android应用程序中单击CardView不会打开活动,而是会导致应用程序崩溃
我是Android开发的新手,刚刚用Java启动了我的第一个Android应用程序。我的应用程序是关于单位转换的,包括显示要转换单位的数量的卡片视图。单击CardView时,用户应被引导到转换发生的部分,但可以看到,单击CardView时,应用程序崩溃。请对这个问题提出适当的解决办法。 MainActivity.java代码- 包com.example.unitconversionappJava 在Android应用程序中单击CardView不会打开活动,而是会导致应用程序崩溃,java,android,Java,Android,我是Android开发的新手,刚刚用Java启动了我的第一个Android应用程序。我的应用程序是关于单位转换的,包括显示要转换单位的数量的卡片视图。单击CardView时,用户应被引导到转换发生的部分,但可以看到,单击CardView时,应用程序崩溃。请对这个问题提出适当的解决办法。 MainActivity.java代码- 包com.example.unitconversionapp import android.content.Intent; import android.os.Build
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
void setActivityTemperature(View view) {
CardView card_view = findViewById(R.id.temperature_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityArea(View view) {
CardView card_view = findViewById(R.id.area_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityTime(View view) {
CardView card_view = findViewById(R.id.time_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivitySpeed(View view) {
CardView card_view = findViewById(R.id.speed_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityWeight(View view) {
CardView card_view = findViewById(R.id.weight_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityEnergy(View view) {
CardView card_view = findViewById(R.id.energy_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityAngle(View view) {
CardView card_view = findViewById(R.id.angle_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityLength(View view) {
CardView card_view = findViewById(R.id.length_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityVolume(View view) {
CardView card_view = findViewById(R.id.volume_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
void setActivityCurrency(View view) {
CardView card_view = findViewById(R.id.currency_card);
card_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
});
}
}
XML代码-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/speed_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivitySpeed"
app:cardBackgroundColor="@color/colorAccent"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/volume_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Speed" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/angle_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityAngle"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/area_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Angle" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/time_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityTime"
app:cardBackgroundColor="@android:color/holo_purple"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:cardMaxElevation="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/angle_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Time" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/volume_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityVolume"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/currency_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Volume" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/area_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityArea"
app:cardBackgroundColor="@android:color/holo_red_light"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/length_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Area" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/currency_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityCurrency"
app:cardBackgroundColor="@android:color/holo_red_dark"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/weight_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Currency" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/weight_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="19dp"
android:onClick="setActivityWeight"
app:cardBackgroundColor="@android:color/holo_green_light"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Weight" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorToolbar"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:subtitleTextColor="#FFFFFF"
app:title="Unit Conversion"
app:titleTextColor="#FFFFFF" />
<androidx.cardview.widget.CardView
android:id="@+id/temperature_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="19dp"
android:onClick="setActivityTemperature"
app:cardBackgroundColor="@android:color/holo_orange_dark"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="setActivityTemperature">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Temperature" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/length_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityLength"
app:cardBackgroundColor="@android:color/holo_blue_dark"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/temperature_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Length" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/energy_card"
android:layout_width="200dp"
android:layout_height="110dp"
android:layout_marginTop="12dp"
android:onClick="setActivityEnergy"
app:cardBackgroundColor="?attr/colorError"
app:cardCornerRadius="25dp"
app:cardElevation="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/speed_card">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Energy" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
您仅在温度、角度、重量和体积
cardwiews
中设置了android:onClick
属性。您还应将其包含在卡片视图的其余部分中,以实现其各自的功能。并像这样公开所有功能public void setActivityWeight(View-View){..}
。
无需再次在java代码中设置侦听器,因为XML中已经有了OnClick
属性。所以你喜欢这样:-
public void setActivityWeight(View view) {
CardView card_view = (CardView) view;
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
因此,您的主要活动将如下所示
MainActivity.java
package com.example.sample;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setActivityTemperature(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityArea(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityTime(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivitySpeed(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityWeight(View view) {
CardView card_view = (CardView) view;
// do whatever you want to do on click (to launch any fragment or activity you need to put intent here.)
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityEnergy(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityAngle(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityVolume(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityCurrency(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
public void setActivityLength(View view) {
CardView card_view = (CardView) view;
Intent intent = new Intent(MainActivity.this, ActivityTemperature.class);
startActivity(intent);
}
}
还要确保将新活动包括在清单文件中
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sample">
<activity android:name="MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityTemperature"/>
</application>
</manifest>
我已经尝试并测试了这段代码,目前运行良好。发布错误StackTrace不要在XML
文件中使用onClick
,尝试在java
文件中创建卡片视图的对象,然后将onClickListner
添加到其中。我也面临着类似的问题。@NadeemSheikh完成了,但仍然面临着同样的问题。顺便说一下,我已经编辑了这篇文章,并根据您的请求添加了错误stacktrace。请看一看it@NadeemSheikh Done@RigvedAlankar嘿,检查答案。我已经更新了它。@NademSheikh我根据您的请求公开了所有功能,但我仍然面临着同样的问题。@NadeemSheikh成功了。我忘了提到清单文件中的活动了。非常感谢你,不是。。。请补充一些解释。。。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**//try this one (Call the function with the current class context, hope that would might help! )**
setActivityWeight(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
**//try this one (Call the function with the current class context, hope that would might help! )**
setActivityWeight(this);
}