单击按钮时,新活动崩溃

时间:2018-03-12 20:14:42

标签: java android-studio

我正在尝试构建一个应用程序来跟踪杂货。我将它分为食物组。打开食物食物组的按钮正常打开,第二个按钮然后崩溃。它被称为乳制品按钮。 每次我点击Dairy按钮时,应用程序都会崩溃。有什么我做错了吗?

这是我的MainActivity.java代码

package apps.fsc.foodtracker;

import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import apps.fsc.foodtracker.R;

// This program exists solely to generate a database and play with it.

public class MainActivity extends AppCompatActivity {
    EditText userEntryText;
    TextView resultsText;
    myDBManager dbManager;
    private Button fruitButton;
    private Button dairyButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fruitButton = findViewById(R.id.fruitButton);
        fruitButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                OpenFruit();
            }

        });

        dairyButton = findViewById(R.id.dairyButton);
        dairyButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                OpenDairy();
            }
        });


        // Grab references to UI elements
        userEntryText = (EditText) findViewById(R.id.userEntryText);
        resultsText = (TextView) findViewById(R.id.resultsText);

        // dbManager reference
        dbManager = new myDBManager(this, null, null, 1);

        // Let's see what's in the database.
        printDatabase();
    }

public void OpenFruit()
    {
        Intent intent1 = new Intent(this, Fruit.class);
        startActivity(intent1);
    }
public void OpenDairy()
    {
        Intent intent2 = new Intent(this, Dairy.class);
        startActivity(intent2);
    }

    public void printDatabase() {
        // Query the database using the dbManager's dbToString() method.
        String dbString = dbManager.dbToString();
        // Drop the results onto the UI
        resultsText.setText(dbString);
        // Set the UI up for fresh input
        userEntryText.setText("");
    }

    public void addButtonClick(View view) {
        // Create a new product based on what's in the UI
        Products product = new Products(userEntryText.getText().toString());
        // Send that product object to the dbManager so the item can be added.
        dbManager.addItem(product);
        // See what's in the database
        printDatabase();

    }

    public void delButtonClick(View view) {
        // Grab the UI contents and dump it into a string
        String inputText = userEntryText.getText().toString();
        // Send that string to the dbManager's delItem() method.
        dbManager.delItem(inputText);
        // See what's in the database
        printDatabase();
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请提供你的清单。 因为您必须在AndroidManifest.xml文件中声明活动,如此

为您的水果活动:

<activity android:name=".Fruit" />

对于您的乳制品活动:

<activity android:name=".Dairy" />

3-12 16:21:25.085 10160-10160 / apps.fsc.foodtracker E / AndroidRuntime:FATAL EXCEPTION:main进程:apps.fsc.foodtracker,PID:10160 android.content.ActivityNotFoundException:无法找到显式活动class {apps.fsc.foodtracker / apps.fsc.foodtracker.Dairy}; *您是否在AndroidManifest.xml中声明了此活动? *

这就是错误信息中所说的内容。