gpt4 book ai didi

Android Button Intent 崩溃

转载 作者:行者123 更新时间:2023-12-03 17:44:16 24 4
gpt4 key购买 nike

我正在 ActionBar 中制作按钮,这将为我打开另一个类。这是我的 MainActivity 代码:

package com.example.menu;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = (Button) findViewById(R.id.add);
Button mest = (Button) findViewById(R.id.miskaste);

ActionBar actionBar = getActionBar();
String dateString = (String) android.text.format.DateFormat.format("yyyy/MM/dd", new java.util.Date());
actionBar.setTitle(dateString);

add = (Button)findViewById(R.id.add);

add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Add.class);
startActivity(intent);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the myouare going to die alone with enu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;



}
}

这是我的 Add 类:
package com.example.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Add extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);

Button orderButton = (Button) findViewById(R.id.but);

orderButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
finish();
}

});
}

}

和 list :
<activity android:name=".Add" />

正如您在代码中看到的那样,我的计划是按下按钮并运行另一个 Activity ,但是当我运行我的应用程序时 - 它崩溃了。所以有人能找到错误吗?我为此工作了两个小时,却一无所获……

这是我的 Logcat:
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:57:39.596: E/AndroidRuntime(622): at android.os.Looper.loop(Looper.java:137)
05-21 11:57:39.596: E/AndroidRuntime(622): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 11:57:39.596: E/AndroidRuntime(622): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:57:39.596: E/AndroidRuntime(622): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:57:39.596: E/AndroidRuntime(622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 11:57:39.596: E/AndroidRuntime(622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 11:57:39.596: E/AndroidRuntime(622): at dalvik.system.NativeStart.main(Native Method)
05-21 11:57:39.596: E/AndroidRuntime(622): Caused by: java.lang.NullPointerException
05-21 11:57:39.596: E/AndroidRuntime(622): at com.example.menu.MainActivity.onCreate(MainActivity.java:27)
05-21 11:57:39.596: E/AndroidRuntime(622): at android.app.Activity.performCreate(Activity.java:4465)
05-21 11:57:39.596: E/AndroidRuntime(622): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 11:57:39.596: E/AndroidRuntime(622): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

最佳答案

如果要启动此添加 Activity ,您必须在 AndroidManifest.xml 中为该添加 Activity 添加此 Intent 过滤器:

<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>

关于Android Button Intent 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16669378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com