如何在适配器中启动活动?我在我的适配器中使用此方法:
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context,asabani_cat.class));
}
});
但这只适用于我的应用程序的片段,当我在Activity App中调用此代码时突然崩溃了!
我有另一个问题,是在适配器中启动活动的好方法吗?
我的logcat:
11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:致命例外:主要11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:java.lang.NullPointerException 11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at com.katibehpayam.mahdi.katibehpayam.adapter_common $ 7.onClick(adapter_common.java:266 )11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.view.View.performClick(View.java:4377)11-27 21:25:25.086 22237-22237 /com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:在android.view.View $ PerformClick.run(View.java:18044)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:725)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler .java:92)11-27 21:25:25.086 22237-22237 / com.katibehpay am.mahdi.katibehpayam E / AndroidRuntime:在android.os.Looper.loop(Looper.java:137)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:在android。 app.ActivityThread.main(ActivityThread.java:5306)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native Method)11 -27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:511)11-27 21:25:25.086 22237-22237 /com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi .katibehpayam E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)11-27 21:25:25.086 22237-22237 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at dalvik .system.NativeStart.main(原生方法)
答案 0 :(得分:4)
你可以尝试:
# install.packages("ggplot2", dependencies = TRUE)
require(ggplot2)
df <- structure(list(Sample = structure(1:12, .Label = c("sample1_A",
"Sample1_B", "sample2_A", "sample2_B", "sample3_A", "sample3_B",
"sample4_A", "sample4_B", "sample5_A", "sample5_B", "sample6_A",
"sample6_B"), class = "factor"), observation1 = c(163453473L,
170151351L, 194102849L, 170642240L, 192858504L, 177174787L, 199232380L,
211128931L, 186039420L, 145855252L, 211165202L, 220522502L),
observation2 = c(131232689L, 137202212L, 162112484L, 141888123L,
161227348L, 147412720L, 174656081L, 186848929L, 152618196L,
118225865L, 186625116L, 193191927L), observation3 = c(61984186L,
59242536L, 89158170L, 79925652L, 90532447L, 81523935L, 118115358L,
123552556L, 87012356L, 66265976L, 112710053L, 114882014L),
percentage = c(30.6236955883, 26.8866816109, 40.4183031852,
41.7493687378, 41.8068248626, 40.5463120438, 55.6409038531,
54.7201927527, 40.9656544833, 39.5744515254, 48.5457722338,
47.238670909)), .Names = c("Sample", "observation1", "observation2",
"observation3", "percentage"), class = "data.frame", row.names = c(NA,
-12L))
# install.packages("reshape2", dependencies = TRUE)
require(reshape2)
data1.long <- melt(df, id=c("Sample"), measure.var = c("observation1", "observation2", "observation3"))
data1.long$percentage <- paste(round(data1.long$percentage, 2), "%", sep="")
data1.long[data1.long$variable == "observation1" | data1.long$variable == "observation2" ,2] <- ""
ggplot(data1.long, aes(x = Sample, y = value, fill=variable)) +
geom_bar(, stat="identity", width=0.5, position="dodge") +
geom_text(aes(label = percentage), vjust=2.10, size=2, hjust=-.06, angle = 90)
答案 1 :(得分:0)
您应该从Logcat发布错误,以便我们进一步帮助您。但是,根据您的显示,我发现您的代码本身没有任何错误。确保正确设置上下文。此外,如果您已参加活动,则不需要说
context.startActivity(new Intent(context,asabani_cat.class));
你可以简单地说
startActivity(new Intent(this, asabani_cat.class));
如果您不在活动中,则需要传递上下文
MyAdapter myAdapter = new MyAdapter(this);
然后在你的适配器中设置你的上下文:
MyAdapter(Context context) {
this.context = context;
}
修改强>
您的logcat显示空指针异常是导致崩溃的原因。你能否在第266行发布adapter_common.java的代码?感谢
答案 2 :(得分:0)
在科特林,您认为:
class YourClass: ArrayAdapter<Feature>(), View.OnClickListener{
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var yourView = inflater.inflate(mResource, parent, false)
yourView .setOnClickListener(){
onClick(convertView)
}
}
override fun onClick(v: View?) {
val intent = Intent(mContext, SeckondActivity::class.java)
mContext.startActivity(intent)
}
}