2012-02-20 74 views
0

我有這樣的代碼:按下一個按鈕後,運行Android的例外前往下一個活動

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

我能想到的唯一問題是您的活動「AddProblemActivity」未在清單中註冊。

查看LogCat下的日誌...你會發現它在窗口>顯示視圖>安卓> LogCat

+0

謝謝 - 是的,可能就是這樣。我如何在清單中註冊? :) – GeekedOut 2012-02-20 13:41:43

+0

btw我打開了LogCat,但它只給出了一行異常,而不是完整的堆棧跟蹤。 – GeekedOut 2012-02-20 13:42:19

+0

只需將其添加到清單,它的工作!謝謝! – GeekedOut 2012-02-20 14:01:23

相關問題