0

我目前有这个和 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清单中设置,下一个活动为空白我得到的日志猫消息是:

RecipeMethodActivity onCreate() 中的错误。

我的 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);

}

}

4

1 回答 1

0

我检查了你的代码..

见下文

公共类 DemoActivity 扩展 Activity 实现 OnClickListener{

私人按钮按钮;

私人 EditText 输入;

    /** 在第一次创建活动时调用。*/
    @覆盖
    公共无效 onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        设置内容视图(R.layout.main);

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

        button.setOnClickListener(this);
    }

    @覆盖
    公共无效 onClick(查看 v){
        // TODO 自动生成的方法存根
        if(v.getId() == R.id.submit) {
            //recipes = (ListView)findViewById(R.array.recipes);
            字符串值 = input.getText().toString();
            Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
            Intent i = new Intent(this, RecipeMethodActivity.class);
            DemoActivity.this.startActivity(i);
        }
    }
}

主要的.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" />

食谱方法活动

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>

于 2012-05-01T05:34:10.450 回答