Android开发应用程序崩溃

时间:2014-06-26 20:26:46

标签: java android eclipse

嗨,伙计们, 我今天开始学习为Android开发应用程序。 我的导游是新波士顿: https://buckysroom.org/videos.php?cat=6&video=16731 我通过每个教程直到10日,因为: 我做了每一步,但应用程序崩溃了。 这是XML代码:

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvDisplay"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Add here" />

    <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="The total is: " />

</RelativeLayout>

以下是JAVA代码:

package com.example.firstpro;



public class MainActivity extends ActionBarActivity {

int c = 0;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add = (Button) findViewById(R.id.bAdd);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            c+=1;
            display.setText("You're total is: " + c);
            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 {

    public PlaceholderFragment() {
    }

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

}

我真的不知道这里有什么问题。即使是警告也没有错误。 我希望得到答案。 感谢。

1 个答案:

答案 0 :(得分:0)

您的按钮和textview采用片段布局,而不是活动布局,因此您必须将代码移至onCreateView:

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 {

    public PlaceholderFragment() {
    }

    int c=0;

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

        Button add = (Button) rootView.findViewById(R.id.bAdd);
        TextView display = (TextView) rootView.findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                c+=1;
                display.setText("You're total is: " + c);
                //you don't need to set same content view again
                //setContentView(R.layout.activity_main);
            }
        });

        return rootView;
    }
}
}