2014-05-03 2 views
0

Я действительно не знаю, в чем проблема с этим ... Приложение должно изменять текст текста только при нажатии кнопки, в этом случае кнопки запуска.Приключение приложения для Android при запуске

Когда я запускаю приложение с виртуальной машиной, он падает.

Мой код

package com.example.projectiii; 

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


    Button btnS = (Button) findViewById(R.id.btStart); 
    btnS.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View view) { 
      // TODO Auto-generated method stub 

      TextView word = (TextView) findViewById(R.id.display); 
      word.setText("Project"); 

     } 

    }); 
} 

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

} 

Мой 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: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.projectiii.MainActivity$PlaceholderFragment" > 

<EditText 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/display" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="46dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/btStart" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/text" 
    android:layout_below="@+id/text" 
    android:layout_marginTop="42dp" 
    android:text="Start" /> 

<Button 
    android:id="@+id/btFinish" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/btStart" 
    android:layout_alignRight="@+id/text" 
    android:text="Finish" /> 

<Chronometer 
    android:id="@+id/time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignRight="@+id/text" 
    android:layout_marginTop="40dp" 
    android:text="Chronometer" /> 

<TextView 
    android:id="@+id/display" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/time" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Text" 
    android:textSize="30sp" /> 

Мой LogCat

05-03 15:06:48.720: E/AndroidRuntime(921): FATAL EXCEPTION: main 
05-03 15:06:48.720: E/AndroidRuntime(921): Process: com.example.projectiii, PID: 921 
05-03 15:06:48.720: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectiii/com.example.projectiii.MainActivity}: java.lang.NullPointerException 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.os.Looper.loop(Looper.java:136) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.main(ActivityThread.java:5017) 
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invokeNative(Native Method) 
05-03 15:06:48.720: E/AndroidRuntime(921): at java.lang.reflect.Method.invoke(Method.java:515) 
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
05-03 15:06:48.720: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
05-03 15:06:48.720: E/AndroidRuntime(921): at dalvik.system.NativeStart.main(Native Method) 
05-03 15:06:48.720: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException 
05-03 15:06:48.720: E/AndroidRuntime(921): at com.example.projectiii.MainActivity.onCreate(MainActivity.java:33) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Activity.performCreate(Activity.java:5231) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
05-03 15:06:48.720: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
05-03 15:06:48.720: E/AndroidRuntime(921): ... 11 more 
05-03 15:06:48.760: W/ActivityManager(385): Force finishing activity com.example.projectiii/.MainActivity 
+0

В ловушке: 05-03 15: 06: 48.720: E/AndroidRuntime (921): вызвано: java.lang.NullPointerException 05-03 15: 06: 48.720: E/AndroidRuntime (921): at com .example.projectiii.MainActivity.onCreate (MainActivity.java:33) 'означает, что в строке 33 было исключение NullPointerException. – Randy

ответ

0

Удостоверьтесь, что xml, который вы опубликовали, activity_main, а не fragment_main. Здесь вам не нужен фрагмент, поэтому удалите все связанные с фрагментами коды.

0

Свяжите вид TextView здесь:

setContentView(R.layout.activity_main); 
TextView word = (TextView) findViewById(R.id.display); 
0

Макет xml, который вы указали, имеет фрагмент (поскольку он не содержит контейнер R.id.). Вы пытаетесь получить доступ к элементу кнопки, определенному в макете фрагмента, в своей деятельности, которая дает вам нулевой указатель. Попробуйте получить доступ к кнопке из класса фрагмента, который разрешит вашу ошибку.

Смежные вопросы