Java 显示错误的意图对象
我正在使用android活动,并尝试在单击前一个活动上的按钮时启动一个活动,问题从intent对象开始,因为第一个参数,即;意向构造函数的上下文参数与“this,MainActivity.this,getApplicationContext(),getBaseContext”不兼容。我尝试了意向构造函数对象的第一个参数中的所有参数。下面是我的代码Java 显示错误的意图对象,java,android,android-intent,Java,Android,Android Intent,我正在使用android活动,并尝试在单击前一个活动上的按钮时启动一个活动,问题从intent对象开始,因为第一个参数,即;意向构造函数的上下文参数与“this,MainActivity.this,getApplicationContext(),getBaseContext”不兼容。我尝试了意向构造函数对象的第一个参数中的所有参数。下面是我的代码 package com.example.nadeemahmad.guitest; import android.content.Intent; i
package com.example.nadeemahmad.guitest;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button ma_sub_btn,prof_sub_btn;
RelativeLayout ma_rel_lay2,ma_rel_lay3,ma_dots_ly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ma_sub_btn = (Button)findViewById(R.id.ma_sub_btn);
ma_rel_lay2 = (RelativeLayout)findViewById(R.id.ma_rel_lay2);
ma_rel_lay3 = (RelativeLayout)findViewById(R.id.ma_rel_lay3);
ma_dots_ly = (RelativeLayout)findViewById(R.id.ma_dots_ly);
ma_sub_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ma_rel_lay2.setVisibility(View.GONE);
ma_rel_lay3.setVisibility(View.VISIBLE);
ma_dots_ly.setVisibility(View.GONE);
}
});
prof_sub_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(this, profile.class);
startActivity(i);
}
});
}
}
您已经在
onClick
中编写了这个。。。尝试写入MainActivity。这个应该可以工作。。有关使用此
关键字的详细信息
Intent i=新的Intent(MainActivity.this、profile.class)代码>
在您的情况下,在观看stacktrace之后,ans应该使用xml绑定prof_sub_btn
按钮 对于按钮prof_sub_btn
,您将获得NullPointerException
。问题是在设置OnClickListener
之前,您没有初始化按钮prof_sub_btn
试试这个:
prof_sub_btn = (Button) findViewById(R.id.prof_sub_btn);
prof_sub_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, profile.class);
startActivity(i);
}
});
在设置内容视图后添加此行:
prof_sub_btn = (Button)findViewById(R.id.prof_sub_btn);
发布堆栈跟踪崩溃日志,在那里你可以看到在android研究的底部发生了什么,也就是“android监视器”。你没有在你的日志中初始化prof_sub_btncode@MuthukrishnanRajendran我确实在你的问题中更新了你的代码,现在它也崩溃了…?再次阅读问题,这是我尝试过的,我在问题中也提到过,通过传递“MainActivity.this,getApplicationContext(),getBaseContext”使我的应用程序崩溃并停止运行Intent i=new Intent(this,profile.class)代码>这一行永远不会工作,因为它在onClick中。您需要做的就是Intent i=newintent(MainActivity.this,profile.class)代码>如果仍然不工作,请发布应用程序终止的堆栈跟踪检查图像链接@NileshDeokarOMG!谢谢,我没有that@KodeGylaxy你读了我的评论吗?我很久以前问过你,你初始化了吗?不管怎样,你解决了你的问题(Y)。@KodeGylaxy非常欢迎亲爱的。如果我的回答似乎有用,那么我希望你投一票。提前谢谢:)也谢谢你@MuthukrishnanRajendran我想你是在问第一句话