2014-06-26 4 views
0

Привет, ребята, Сегодня я начал изучать приложение для Android. Мой гид - новый Бостон: https://buckysroom.org/videos.php?cat=6&video=16731 Я прошел каждый урок до 10-го числа, потому что: Я делал каждый шаг, но приложение вылетает. Вот код XML:Android Developing App crashes

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

} 

Я действительно не знаю, что здесь проблема. Предупреждений нет даже. Надеюсь на ответы. Спасибо.

+5

с каким сообщением? – Kon

+0

Пахнет http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto

+0

Эй, Конец, проверьте это: http://prntscr.com/3wt2pl. – iYonatan

ответ

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

Создайте новый onCreateView? или заменить его в существующих onCreateView? – iYonatan

+0

Ну, просто поместите код в onCreateView, как я сделал, и удалим его из onCreate – Zoran

+0

ok Я пытаюсь это сделать сейчас. Я оставлю здесь комментарий, если он работает или нет. – iYonatan