0

我正在 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)
4

3 回答 3

0

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

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
</intent-filter>
于 2013-05-21T12:09:55.057 回答
0

假设您正在尝试按照您提到的那样调用操作栏中的按钮

i need it to be in actionbar

并且您提到了您的选项菜单项

    <item android:id="@+id/add" 
android:icon="@drawable/add" 
android:showAsAction="ifRoom|withText" />

尝试在您的代码中调用它

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.add:
      Intent intent = new Intent(MainActivity.this, Add.class);
                startActivity(intent);
      break;  

    default:     

      break;
    }

    return true;
  }
于 2013-05-21T12:53:26.050 回答
-1

你的“布局”文件夹中有一个文件activity_main,你应该在其中创建你的按钮,如下所示:

<Button
            android:id="@+id/btn_next"
            style="@style/CameraButtonText"
            android:layout_marginRight="5dp"
            android:background="@layout/nextbutton"
            android:onClick="overviewClick"
            android:text="@string/next" />

如果你想要它在操作栏中,你必须给它充气:http: //developer.android.com/guide/topics/ui/actionbar.html

一切都可以在链接中找到

于 2013-05-21T12:22:59.460 回答