【问题标题】:RuntimeError for activity specified on Android ManifestAndroid Manifest 上指定的活动的 RuntimeError
【发布时间】:2014-03-14 18:32:20
【问题描述】:

我遇到了这个看起来很奇怪的问题。 我有这个带有六个按钮的 Android 视图,当我单击其中一个按钮(除了退出按钮之外唯一一个定义了操作的按钮)时,它给了我一个 RuntimeError。 活动在清单上声明,这就是为什么我不明白我的错误。 有人可以帮我解决这个问题。 我真的很感激:)

这是我的代码:

package com.example.calendar;

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

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View createButton = findViewById(R.id.btn_create);
    View deleteButton = findViewById(R.id.btn_delete);
    View moveButton = findViewById(R.id.btn_move);
    View searchButton = findViewById(R.id.btn_search);
    View translateButton = findViewById(R.id.btn_translate);
    View exitButton = findViewById(R.id.exit);
    createButton.setOnClickListener(this);
    deleteButton.setOnClickListener(this);
    moveButton.setOnClickListener(this);
    searchButton.setOnClickListener(this);
    translateButton.setOnClickListener(this);
    exitButton.setOnClickListener(this);
}

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_create:
    Intent i = new Intent(this, CreateAppointment.class);
    startActivity(i);
    break;
    case R.id.exit:
        finish();
        break;

    }

}

}

我的布局是这样的:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@color/background"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="225dp"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" >

            <CalendarView
                android:id="@+id/calendar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="2dip"
            android:layout_marginTop="2dip"
            android:stretchColumns="*" >

            <TableRow>

                <Button
                    android:id="@+id/btn_create"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/create" />

                <Button
                    android:id="@+id/btn_viewEdit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/viewEdit" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/delete" />

                <Button
                    android:id="@+id/btn_move"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/move" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/btn_search"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/search" />

                <Button
                    android:id="@+id/btn_translate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/translate" />
            </TableRow>
        </TableLayout>

        <Button
            android:id="@+id/exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit"/>

</LinearLayout>

还有清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calendar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.calendar.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CreateAppointment"
            android:theme="@android:style/Theme.Dialog"
            android:label="@string/create" >
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 你能在你的问题中添加堆栈跟踪吗?
  • 堆栈跟踪?请原谅我的无知,但堆栈跟踪是什么? @ZouZou
  • 在 logcat 中,您说您遇到了 RuntimeError。能否请您发布此错误的完整日志?
  • 快速扫描代码看起来不错,我猜需要你的堆栈跟踪。如果您找不到您的 LogCat(并且正在使用 Eclise),请转到 Window -> Show View -> LogCat。
  • com.example.calendar at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)

标签: java android button android-activity android-manifest


【解决方案1】:

您必须正确定义 onCreate() 中使用的视图类型

 View createButton = findViewById(R.id.btn_create);

例如:

因为您使用的是按钮:

  <Button
                    android:id="@+id/btn_create"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/create" />

将演员转换为按钮:

 Button createButton = (Button)findViewById(R.id.btn_create);

试试这个:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button createButton = (Button)findViewById(R.id.btn_create);
    Button deleteButton = (Button)findViewById(R.id.btn_delete);
    Button moveButton = (Button)findViewById(R.id.btn_move);
    Button searchButton = (Button)findViewById(R.id.btn_search);
    Button translateButton = (Button)findViewById(R.id.btn_translate);
    Button exitButton = (Button)findViewById(R.id.exit);
    createButton.setOnClickListener(this);
    deleteButton.setOnClickListener(this);
    moveButton.setOnClickListener(this);
    searchButton.setOnClickListener(this);
    translateButton.setOnClickListener(this);
    exitButton.setOnClickListener(this);
}

【讨论】:

  • 感谢您的额外投入。马上改。但是,我可以同时找到错误:)
  • View createButton = findViewById(R.id.btn_create); 如果您只需要 View 类中的方法,则非常好(虽然不常用)
猜你喜欢
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多