按下按钮后转到下一个Activity的运行时Android异常

时间:2012-02-20 13:30:18

标签: android android-layout android-intent

我有这段代码:

package com.problemio;

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

public class ProblemioActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button addProblemButton = (Button)findViewById(R.id.add_problem_button);   
        Button browseProblemsButton = (Button)findViewById(R.id.browse_problems_button);   
        Button searchProblemsButton = (Button)findViewById(R.id.search_problems_button);   
        Button myProblemsButton = (Button)findViewById(R.id.my_problems_button);           

        addProblemButton.setOnClickListener(new Button.OnClickListener() 
        {  
            public void onClick(View v) {
              Intent myIntent = new Intent(ProblemioActivity.this, AddProblemActivity.class);
              ProblemioActivity.this.startActivity(myIntent);
            }
        });        
    }
}

它编译得很好并显示addProblemButton按钮,但是当单击该按钮时,系统会给出运行时异常。

这是AddProblemActivity类:

package com.problemio;

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

public class AddProblemActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //TextView text = (TextView) dialog.findViewById(R.id.addProblemText);
        //text.setText(R.string.addProblemText);        

        TextView tv = new TextView(this);
        tv.setText("Please Add a Problem");
        setContentView(tv);        
    }
}

这是布局main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="First, add the problem you want to solve!"
    />

<TextView
    android:id="@+id/add_problem_text"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add a Problem You Want To See Solved"
    />    

    <Button
    android:id="@+id/add_problem_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add a Problem"
    />

    <Button
    android:id="@+id/browse_problems_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Browse Problems"

    />
    <Button
    android:id="@+id/search_problems_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Search Problems"
    />

    <Button
    android:id="@+id/my_problems_button"        
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="View My Problems"
    />    
</LinearLayout>

知道可能出现什么问题?顺便说一句,我似乎无法找到异常的堆栈跟踪。我应该在Eclipse中寻找哪个位置?它目前显示的只是AndroidRuntimeException - dalvik.system.NativeStart.main

谢谢!

1 个答案:

答案 0 :(得分:1)

我能想到的唯一问题是您的活动“AddProblemActivity”未在清单中注册。

查看LogCat下的日志...您可以在Window&gt; Show View&gt; Android&gt;中找到它。 logcat的