[英]New activity not starting in Android app
I am trying to start a new activity on button click.我正在尝试在单击按钮时开始一项新活动。 I am doing this at various places in my application.
我在我的应用程序的各个地方都这样做。 1st when the user is logging into the system.
1st 当用户登录系统时。 I am using following code for opening new activity:
我正在使用以下代码打开新活动:
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check if Name text control is not empty
if (etUserName.getText().length() != 0 && etPassword.getText().toString() != "") {
//code to check login is correct or not
Context ctx = v.getContext();
Intent intent = new Intent(ctx, DashboardActivity.class);
ctx.startActivity(intent);
} else {
// tv.setText("Please enter name");
}
}
});
This is working fine and DashboardActivity is opened.这工作正常并且 DashboardActivity 已打开。 And on this new screen I have three button, on click of each button a new activity starts.. Code for one of the button is like:
在这个新屏幕上,我有三个按钮,单击每个按钮会启动一个新活动。其中一个按钮的代码如下:
btnMyActivity = (Button) findViewById(R.id.btnMyActivity);
btnFarmerStatus = (Button) findViewById(R.id.btnFarmerStatus);
btnMeetings = (Button) findViewById(R.id.btnMeetings);
btnMyActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check if Name text control is not empty
Context ctx = v.getContext();
Intent intent = new Intent(v.getContext(), MyactivityActivity.class);
ctx.startActivity(intent);
}
});
Manifest xml is like:清单 xml 是这样的:
<activity android:name=".DashboardActivity" android:screenOrientation="portrait" />
<activity android:name=".MyactivityActivity" android:screenOrientation="portrait" />
<activity android:name=".FarmerstatusActivity" android:screenOrientation="portrait" />
Somehow, the code is same but second-time the new activity is not opening.不知何故,代码是相同的,但第二次新活动没有打开。 Infact nothing happens on the screen.
事实上,屏幕上什么也没有发生。 What can be wrong in my code?
我的代码有什么问题?
Update: When DashboardActivity opens logcat shows:更新:当 DashboardActivity 打开 logcat 时显示:
08-05 15:33:04.880 3073-3073/com.it.allied.ots_dashboard I/Choreographer: Skipped 55 frames!
08-05 15:33:04.880 3073-3073/com.it.allied.ots_dashboard I/Choreographer:跳过了 55 帧! The application may be doing too much work on its main thread.
应用程序可能在其主线程上做了太多工作。 08-05 15:33:05.279 3073-3138/com.it.allied.ots_dashboard E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabda5950
08-05 15:33:05.279 3073-3138/com.it.allied.ots_dashboard E/Surface:getSlotFromBufferLocked:未知缓冲区:0xabda5950
And Here is the code from Myactivityactivity class这是来自 Myactivityactivity 类的代码
public class MyactivityActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.activity_myactivity);
}
} }
btnMyActivity = (Button) findViewById(R.id.btnMyActivity);
btnFarmerStatus = (Button) findViewById(R.id.btnFarmerStatus);
btnMeetings = (Button) findViewById(R.id.btnMeetings);
btnMyActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check if Name text control is not empty
Intent intent = new Intent(context, MyactivityActivity.class);
startActivity(intent);
}
});
I think this will help you.try this.我想这会帮助你。试试这个。
It seems that other answers here are not helping you out.似乎这里的其他答案对您没有帮助。 Try this :
试试这个:
The other way of creating an Intent is by using the AndroidManifest.xml file.创建 Intent 的另一种方法是使用 AndroidManifest.xml 文件。
Steps:步骤:
1) Open the AndroidManifest.xml
file and move to : 1) 打开
AndroidManifest.xml
文件并移动到:
<activity android:name=".MyactivityActivity"> </activity>
2) Now use the intent-filter tag like this : 2)现在使用这样的意图过滤器标签:
<activity android:name=".MyactivityActivity">
<intent-filter>
</intent-filter>
</activity>
3) Change the action name, eg: 3)更改动作名称,例如:
<action android:name="AnythingOfYourChoice" />
(but no space) The ideal way of writing the action name goes like this : (但没有空格)写动作名称的理想方式是这样的:
<action android:name="packagename.ActivityName" />
4) Change the category name, eg: 4)更改类别名称,例如:
<category android:name="android.intent.category.DEFAULT" />
5) Then use the Implicit Intent 5)然后使用隐式意图
Intent i = new Intent("packagename.ActivityName");
startActivity(i);
This may solve your problem.这可能会解决您的问题。
First Solution : Define Context context;
第一个解决方案:定义
Context context;
in your activity and context = this;
在您的活动和
context = this;
in onCreate() Method of your activity.在您的活动的 onCreate() 方法中。
btnMyActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, MyactivityActivity.class);
startActivity(intent);
}
});
And并且
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check if Name text control is not empty
if (etUserName.getText().length() != 0 && !(etPassword.getText().toString().equals(""))) {
//code to check login is correct or not
Intent intent = new Intent(context, DashboardActivity.class);
startActivity(intent);
} else {
// tv.setText("Please enter name");
}
}
});
Second Solution : if you don't want to use context
then just replace the context
in Intent
with yourActivity.this
.第二种解决方案:如果您不想使用
context
那么只需将Intent
的context
替换为yourActivity.this
。
Intent intent = new Intent(MyactivityActivity.this, DashboardActivity.class);
startActivity(intent);
i think you check if我想你检查一下
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (etUserName.getText().length() != 0 && !(etPassword.getText().toString().equals(""))) {
Context ctx = v.getContext();
Intent intent = new Intent(ctx, DashboardActivity.class);
ctx.startActivity(intent);
} else {
// tv.setText("Please enter name");
}
}
});
I looks like it is issue with Android 6.0.我看起来像是 Android 6.0 的问题。 I am trying to update android emulator and it is not working.
我正在尝试更新 android 模拟器,但它不起作用。 I am starting a new thread for android emulator issue.
我正在为 android 模拟器问题启动一个新线程。
I had the exact same issue.我有完全相同的问题。 What I found was that I had written ListView instead of LinearLayout in the xml file.
我发现我在xml文件中编写了ListView而不是LinearLayout。 Always check for the layouts you used in the xml, the issue lies there only.
始终检查您在 xml 中使用的布局,问题仅存在于此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.