【问题标题】:unique Fatal Exception: main (Unfortunately app has stopped error) [duplicate]独特的致命异常:主要(不幸的是应用程序已停止错误)[重复]
【发布时间】:2014-07-21 04:35:39
【问题描述】:

编辑:我将内容视图更改为 setContentView(R.layout.fragment_main);应用程序仍然崩溃。我不太明白这是为什么。

这是一个简单的应用程序,用于检测和计算道路上感觉到的坑洼数量。我已经阅读了很多关于这个问题的主题,并对我的代码进行了许多不同的更改。但似乎没有一个能解决我遇到的错误。我的代码到底哪里出错了?

MainActivity.java

package com.example.potholedetector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements Button.OnClickListener {


    // Private member field to keep track of the number of potholes
    private static int num_potholes = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        //Restore any saved state
        super.onCreate(savedInstanceState);

        //Set content view
        setContentView(R.layout.activity_main);

        //Initialize UI elements 
        final Button pothole = (Button)findViewById(R.id.pothole);
        final TextView NumPotholes = (TextView)findViewById(R.id.NumPotholes);

        //Link UI elements to actions in code
            pothole.setOnClickListener(new Button.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    num_potholes++;
                    NumPotholes.setText("Number of potholes felt: " + num_potholes);
                }
            });

        if (savedInstanceState == null) 
        {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) 
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment 
    {

        public PlaceholderFragment() 
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) 
        {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;

        /** Called when the user clicks the Pothole button */

        }      
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }


}

fragment_main.xml

<LinearLayout 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:orientation="vertical"
    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.potholedetector.MainActivity$PlaceholderFragment" >

    <TextView android:id="@+id/pothole_message"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="@string/pothole_message"
        android:textSize="20sp"/>
    <Button android:id="@+id/pothole"
        android:layout_width="wrap_content"
        android:minWidth="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/pothole"
        android:onClick="potholeFelt"/>
    <TextView android:id="@+id/NumPotholes"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="20sp"/>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">PotholeDetector</string>
    <string name="pothole_message">Pothole felt?</string>
    <string name="pothole">Pothole</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

我尝试仔细遵循许多教程,但是在花了这么多时间调试之后,我离消除错误并继续前进还差得远。我怎样才能使这段代码工作?

【问题讨论】:

  • 您应该将所有 Button 参考代码移到 Fragment 下。看看我上面给出的链接。
  • 我之前调查过。这让我有点困惑,因为根据一些教程,这个 java 代码应该在 MainActivity.java 中,而不是在 fragment.xml 中。当我将 pothole.setOnClickListener 代码(“将 UI 元素链接到代码中的操作”下的块)放在 fragment.xml 中时,我收到一条警告说“在布局文件中找到了意外的文本”。这就是你的意思吗?

标签: java android eclipse fatal-error


【解决方案1】:

您的应用因 NPE 而崩溃,因为您将 R.layout.activity_main 作为参数传递给 setContentView,但您要查找的元素在 fragment_main.xml 中声明。致电setContentView(R.layout.fragment_main);

【讨论】:

  • 我把它改成了 setContentView(R.layout.fragment_main);但这似乎并不能解决问题。应用程序仍然崩溃。我做错了什么吗?
猜你喜欢
  • 2012-08-23
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多