编辑:我将内容视图更改为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>
我试着仔细地按照许多教程,但在花了这么多时间调试之后,我就没有更接近根除错误并继续前进。如何使这段代码有效?
答案 0 :(得分:1)
您的应用程序因NPE而崩溃,因为您将参数R.layout.activity_main
传递给setContentView
,但您要查找的元素在fragment_main.xml
内声明。致电setContentView(R.layout.fragment_main);