我正在 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();
}
});
}
}
和清单:
<activity android:name=".Add" />
正如您在代码中看到的那样,我的计划是按下按钮并运行另一个活动,但是当我运行我的应用程序时 - 它崩溃了。所以有人能找到错误吗?我为此工作了两个小时,却一无所获……
这是我的 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)