未找到来源错误和应用程序突然关闭

时间:2014-12-11 16:25:53

标签: java android debugging button android-intent

我是Android编程的新手,现在已经搜索了一天这个问题并且无法提出解决方案。请帮助我解决这个给我带来大问题的小问题。

错误是,当我按下MainActivity中的登录按钮时,应用程序说不幸的是它已停止工作,调试窗口显示错误"Source not found",并进一步显示{{1} }

我看过与此相关的类似帖子,但这些帖子无法帮助我 按照我的代码。

清单

ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2249

MainActivity.java

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

   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="17" />

   <application
       android:allowBackup="true"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" >


       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       <activity
           android:name=".Second"
           android:label="@string/title_activity_second" >
       </activity>
       <activity
           android:name=".Registration"
           android:label="@string/title_activity_registration" >
       </activity>
   </application>

   </manifest>

MainActivity_xml

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,Second.class);
                startActivity(i);
            }
        });
        final Button button2=(Button) findViewById(R.id.button2_id);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent j = new Intent(MainActivity.this,Registration.class);
                startActivity(j);
            }
        });
    }
}

activity_second.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.firstapp.MainActivity" >

 <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView1"
    android:text="Welcome To Foodparkk"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button_id"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="45dp"
    android:contentDescription="@null"
    android:src="@drawable/robo" />

<Button
    android:id="@+id/button_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textView3"
    android:text="Login" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_alignTop="@+id/button2_id"
    android:text="E-Mail"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="19dp"
    android:text="Password"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:ems="10"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:ems="10"
    android:inputType="textEmailAddress" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button2_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button_id"
    android:layout_alignRight="@+id/button_id"
    android:layout_marginBottom="42dp"
    android:text="Register" />

 </RelativeLayout>

Second.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.example.firstapp.Second">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="44dp"
    android:text="Select your Meal"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp" />

 </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

在你的second.java类中,你引用了主要活动的布局

public class Second extends ActionBarActivity implements OnItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

将SetContentView更改为

setContentView(R.layout.activity_second);

答案 1 :(得分:0)

A&#34;未找到来源&#34;无法找到或实例化类或活动时会抛出错误。

请添加完整 LogCat日志或至少添加完整的ErrorStack。