按钮OnClickListener意外停止

时间:2015-03-15 12:58:08

标签: android android-intent switch-statement

当我点击"插入"我已经意外停止了问题。和"更新"。第一个按钮MovieList工作正常。我无法找到导致它停止的错误。

这是我的代码。

MainActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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);

        findViewById(R.id.MovieList).setOnClickListener(click);
        findViewById(R.id.Insert).setOnClickListener(click);
        findViewById(R.id.Update).setOnClickListener(click);
        findViewById(R.id.Delete).setOnClickListener(click);

    }
Button.OnClickListener click = new View.OnClickListener() {

        @Override
        public void onClick(View l) {
            // TODO Auto-generated method stub
            switch(l.getId()){
            case R.id.MovieList:
                Intent s = new Intent(MainActivity.this, list.class);
                startActivity(s);
                break;

            case R.id.Insert:
                Intent i = new Intent(MainActivity.this, insert.class);
                startActivity(i);
                break;

            case R.id.Update:
                Intent u = new Intent(MainActivity.this, update.class);
                startActivity(u);
                break;

            case R.id.Delete:

                break;
            }
        }
    };

我的插入类是空的,我刚刚创建了一个edittext来查看页面是否可以打开。

insert.java:

package com.example.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;

public class insert extends Activity{

    EditText insET;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.id.Insert);

        insET = (EditText) findViewById(R.id.insertET);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

}

logcat的:

03-15 13:02:34.732: E/AndroidRuntime(775): FATAL EXCEPTION: main
03-15 13:02:34.732: E/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.insert}: android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:137)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-15 13:02:34.732: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
03-15 13:02:34.732: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:511)
03-15 13:02:34.732: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-15 13:02:34.732: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-15 13:02:34.732: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)
03-15 13:02:34.732: E/AndroidRuntime(775): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.content.res.Resources.getLayout(Resources.java:853)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-15 13:02:34.732: E/AndroidRuntime(775):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.Activity.setContentView(Activity.java:1881)
03-15 13:02:34.732: E/AndroidRuntime(775):  at com.example.project.insert.onCreate(insert.java:15)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.Activity.performCreate(Activity.java:5104)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-15 13:02:34.732: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-15 13:02:34.732: E/AndroidRuntime(775):  ... 11 more
03-15 13:03:50.915: I/Process(775): Sending signal. PID: 775 SIG: 9

1 个答案:

答案 0 :(得分:2)

问题是{p> setContentView(R.id.Insert);;你应该使用布局资源文件,例如

setContentView(R.layout.insert_layout);

其中insert_layout.xml位于res / layout资源文件夹中,并定义了您的活动的视图层次结构。