我是Android的新手,我正在使用Android studio。我创建了一个带有两个按钮的基本程序。但是一旦我启动了模拟器,应用程序就无法启动。我收到一条消息说'不幸 - 项目名称 - 已停止'。但是编译的代码没有任何错误。这里我附上了代码。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectdrogo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.projectdrogo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的MainActivity.java代码
package com.example.projectdrogo;
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.os.Build;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter;
Button add,subtract;
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter =0;
add = (Button)findViewById(R.id.bAdd);
subtract=(Button)findViewById(R.id.bSubtract);
textview=(TextView)findViewById(R.id.textView);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter++;
textview.setText("Your Total is "+counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter--;
textview.setText("Your Total is "+counter);
}
});
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;
}
}
}
这是我的main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.projectdrogo.MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.projectdrogo.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.projectdrogo.MainActivity$PlaceholderFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<TextView
android:text="@string/name"
android:textSize="50dp"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:id="@+id/bSubtract"
android:textSize="30dp"
android:layout_gravity="center"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Add One"
android:id="@+id/bAdd"
android:layout_gravity="center"
android:textSize="30dp" />
</LinearLayout>
</RelativeLayout>
请帮助我。我陷入了困境。 谢谢
答案 0 :(得分:1)
activity_main.xml
没有按钮或textview。看起来像片段布局
我猜你有一个片段
PlaceholderFragment newFragment = new PlaceholderFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
然后在onCreateView of Fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
add = (Button)rootView.findViewById(R.id.bAdd);
subtract=(Button)rootView.findViewById(R.id.bSubtract);
textview=(TextView)rootView.findViewById(R.id.name);
...// rest of the code
你有FrameLayout
这是一个容器。您需要将片段添加到容器中。活动中的视图也属于片段布局。因此,您需要按照建议在onCreateView中初始化相同内容。
编辑:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlaceholderFragment newFragment = new PlaceholderFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
然后
public static class PlaceholderFragment extends Fragment {
int counter;
Button add,subtract;
TextView textview;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
add = (Button)rootView.findViewById(R.id.bAdd);
subtract=(Button)rootView.findViewById(R.id.bSubtract);
textview=(TextView)rootView.findViewById(R.id.name);
... // rest of the code
return rootView;
}
}