2014-03-03 59 views
0

我是新来的android和我正在使用Android studio.I创建了一个基本的程序与两个按钮。但一旦我开始模拟器的应用程序不启动。我'我收到一条消息,说'不幸的是,项目名称已经停止'。但代码编译没有任何错误。我在附上代码。不幸的是<项目名称>已停止消息

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> 

请帮助me.I'm停留在此。 谢谢

+2

是否有来自logcat的堆栈跟踪? – PearsonArtPhoto

+0

如何在Android工作室中找到LogCat?谢谢 – user3230212

+0

如果您可以在日志猫字段中指定错误,它将使用更容易找到问题的方法... –

回答

1

activity_main.xml没有按钮或textviews。看起来像它在片段布局

我猜你在onCreateView有一个片段

PlaceholderFragment newFragment = new PlaceholderFragment(); 
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
transaction.add(R.id.container, newFragment); 
transaction.addToBackStack(null); 
transaction.commit(); 

然后片段

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(); 
} 

然后

公共静态类PlaceholderFragment扩展片段{

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; 
    } 
} 
+0

对不起。我不明白这一点。请help.Thanks – user3230212

+0

@ user3230212什么是你不明白 – Raghunandan

+0

@ user3230212发布编辑其余的是你找出并指定你不明白 – Raghunandan

相关问题