我必须将MainActivity.java的详细信息发送到另一个活动并在那里打印
我在mainActivity中创建了一个onclick事件,但是当我点击按钮时,应用程序在模拟器中崩溃并在logcat中获取异常
MainActivity.java
Button buy_button = (Button)convertView.findViewById(R.id.buy_global);
buy_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context;
Intent intent = new Intent("com.coded.sandeep.Another");
intent.putExtra("message", "Hello From MainActivity");
Bundle extras = new Bundle();
extras.putString("status", "Data Received!");
intent.putExtras(extras);
context.startActivity(intent);
}
});
Another.java
public class Another extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
// 1. get passed intent
Intent intent = getIntent();
// 2. get message value from intent
String message = intent.getStringExtra("message");
// 3. show message on textView
((TextView)findViewById(R.id.tvMessage)).setText(message);
// 5. get status value from bundle
String status = bundle.getString("status");
// 6. show status on Toast
Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
toast.show();
}
}
logcat的
06-03 05:41:52.249: W/dalvikvm(1644): threadid=1: thread exiting with uncaught exception (group=0xb4a38ba8)
06-03 05:41:52.379: E/AndroidRuntime(1644): FATAL EXCEPTION: main
06-03 05:41:52.379: E/AndroidRuntime(1644): Process: com.coded.sandeep, PID: 1644
06-03 05:41:52.379: E/AndroidRuntime(1644): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.coded.sandeep.Another (has extras) }
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Activity.startActivityForResult(Activity.java:3424)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Activity.startActivityForResult(Activity.java:3385)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Activity.startActivity(Activity.java:3627)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.Activity.startActivity(Activity.java:3595)
06-03 05:41:52.379: E/AndroidRuntime(1644): at com.coded.sandeep.MusicDatabaseActivity$ImageAdapter$3.onClick(MusicDatabaseActivity.java:252)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.view.View.performClick(View.java:4438)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.view.View$PerformClick.run(View.java:18422)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.os.Handler.handleCallback(Handler.java:733)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.os.Handler.dispatchMessage(Handler.java:95)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.os.Looper.loop(Looper.java:136)
06-03 05:41:52.379: E/AndroidRuntime(1644): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-03 05:41:52.379: E/AndroidRuntime(1644): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 05:41:52.379: E/AndroidRuntime(1644): at java.lang.reflect.Method.invoke(Method.java:515)
06-03 05:41:52.379: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-03 05:41:52.379: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-03 05:41:52.379: E/AndroidRuntime(1644): at dalvik.system.NativeStart.main(Native Method)
如果我改变任何东西,代码中是否有任何错误
答案 0 :(得分:3)
替换Intent intent = new Intent("com.coded.sandeep.Another");
与
Intent intent = new Intent(MainActivity.this, Another.class);
使用Intent("com.coded.sandeep.Another");
创建意图会创建'com.coded.sandeep.Another'
类型的意图。但是,由于这是一个自定义意图并且android不知道如何处理它,因此会生成异常。
编辑1: 由于在单击侦听器内部,MainActivity.this实例不可用,请尝试:
Activity activity; //Instance variable
在OnCreate()中:
activity = this;
然后使用Intent intent = new Intent(activity, Another.class);
编辑2:
由于这是在适配器内,并且您的适配器没有启动活动的上下文,因此您可以在初始化适配器时传递上下文,然后使用它来启动活动。
public class CustomAdapter extends BaseAdapter {
Activity activity;
public CustomAdapter(Activity) {
this.activity = activity;
}
//Now use this activity to create a new intent and start new activity inside your button click listener.
}
答案 1 :(得分:1)
尝试以下代码: -
更改以下代码
Intent intent = new Intent("com.coded.sandeep.Another");
到
Intent intent = new Intent(MainActivity.this,Another.class);
答案 2 :(得分:1)
您需要像这样更改您的Intent代码。 Intent intent = new Intent(this,Another.class);
答案 3 :(得分:1)
替换此
Intent intent = new Intent("com.coded.sandeep.Another");
带
Intent intent = new Intent(MainActivity.this,Another.class);
MainActivity.this是来源 Another.class是目的地 简单地说从(MainActivity)到(Another)
答案 4 :(得分:1)
您也可以在 manifest.xml 中更改以下内容,而不是更改代码:
<activity
android:name=".Another" >
<intent-filter>
<action android:name="com.coded.sandeep.Another" />
</intent-filter>
</activity>
希望有所帮助:)