按钮单击后活动崩溃

时间:2012-05-01 00:22:58

标签: android android-activity oncreate

我目前有这个和android活动,但是当点击按钮时,应用程序崩溃了。我找不到什么;错了。

public class SearchActivity extends Activity implements OnClickListener{

private ListView recipes;
Intent intent;
Button button;
EditText input;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.searchlist);

    button = (Button)findViewById(R.id.submit);
    input = (EditText)findViewById(R.id.recipeName);

    //recipes = (ListView)findViewById(R.id.recipes);
    //recipes.setAdapter(new ArrayAdapter<String> (this, R.layout.main, getResources().getStringArray(R.array.BaconSandwich)));
    button.setOnClickListener(this);
}

public void onClick(View clicked) {
    if(clicked.getId() == R.id.submit) {
        //recipes = (ListView)findViewById(R.array.recipes);
        String value = input.getText().toString();
        Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
        Intent i = new Intent(this, RecipeMethodActivity.class);
        SearchActivity.this.startActivity(i);
    }
}

}

活动在android清单中设置,下一个活动为空 我得到的log cat消息是:

RecipeMethodActivity onCreate()中的错误。

My RecipeMethodActivity是:

package com.finalyearproject.cookmefood;

import android.app.ListActivity;
import android.os.Bundle;

public class RecipeMethodActivity extends ListActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

}

}

1 个答案:

答案 0 :(得分:0)


我检查了你的代码..

见下文


public class DemoActivity extends Activity implements OnClickListener{

private Button button;

private EditText input;

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

        button = (Button)findViewById(R.id.submit);
        input = (EditText)findViewById(R.id.recipeName);

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId() == R.id.submit) {
            //recipes = (ListView)findViewById(R.array.recipes);
            String value = input.getText().toString();
            Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
            Intent i = new Intent(this, RecipeMethodActivity.class);
            DemoActivity.this.startActivity(i);
        }
    }
}

main.xml中

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
<EditText
    android:id="@+id/recipeName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <requestFocus />
</EditText>
<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:id="@+id/recipeName" android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />

RecipeMethodActivity

package com.Demo;

import android.app.ListActivity; import android.os.Bundle;

public class RecipeMethodActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_layout);

}

}

new_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/@android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

manifest file:

<uses-sdk android:minSdkVersion="3" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".DemoActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RecipeMethodActivity"></activity>
</application>

相关问题