Android应用程序在运行时停止

时间:2014-04-20 09:15:51

标签: android

我是android和java的新手。这是我的第一个应用程序。一本书的例子。我按照这本书做了所有步骤。该应用程序将通过单击+按钮增加数量 这是我的Java文件:

    package com.example.myprayercounter;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//

        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 {

        int counter;
        TextView tView;
        Button btn;
        public PlaceholderFragment() {}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            counter = 0;
            tView = (TextView)getActivity().findViewById(R.id.txt_textTwo);
            btn = (Button)getActivity().findViewById(R.id.btn_buttonOne);
            btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    counter++;
                    tView.setText(" " + counter);

                }
            });

            return rootView;
        }
    }

}

xml文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fbfbdd"
    android:gravity="center_horizontal"
    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.myprayercounter.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/txt_textOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="تعداد ذکرهای من"
        android:textColor="#763f05"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/txt_textTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="#763f05"
        android:textSize="40dp" />

    <Button
        android:id="@+id/btn_buttonOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#763f05"
        android:text="+"
        android:textColor="#fbfbdd"
        android:textSize="40dp" />

</LinearLayout>

首次运行应用时出现此错误: enter image description here

我的错误是什么?以及我如何解决它们? 提前致谢

1 个答案:

答案 0 :(得分:3)

变化:

tView = (TextView)getActivity().findViewById(R.id.txt_textTwo);
btn = (Button)getActivity().findViewById(R.id.btn_buttonOne);

to(getActivity() - &gt; rootView)

tView = (TextView)rootView.findViewById(R.id.txt_textTwo);
btn = (Button)rootView.findViewById(R.id.btn_buttonOne);
PlaceholderFragment

,您的视图为rootView,您需要从中找到您的观点