-1

Whenever I try to run my Android app made in Eclipse it gives me Has stopped working. It's a simple button and text view text setting. Here is my code.

 package com.example.polidoesnttwantacracker;
 import android.support.v7.app.ActionBarActivity;
 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.widget.Button;
 import android.widget.TextView;

 public class MainActivity extends ActionBarActivity {

private static final Button Button = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView v = (TextView)findViewById(R.id.Maikati);
    v.setText("Welcome you Dick.");
    Button but = (Button)findViewById(R.id.ButtonaNaMaikaTi);
    but.setText("WTF");

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

 }

The xml file as requested.

<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.polidoesnttwantacracker.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/Maikati"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="149dp"
    android:text="" />

<Button
    android:id="@+id/ButtonaNaMaikaTi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Maikati"
    android:layout_centerHorizontal="true"
    android:text="OK" />

  </RelativeLayout>

So if you can please help me. It's really frustrating and I can't go on with my lections if i don't get it running.

Here is the cat log that it gives me :D

06-01 19:05:28.438: E/Trace(2561): error opening trace file: No such file or directory (2)
06-01 19:05:28.534: D/AndroidRuntime(2561): Shutting down VM
06-01 19:05:28.534: W/dalvikvm(2561): threadid=1: thread exiting with uncaught exception (group=0xa61f4908)
06-01 19:05:28.538: E/AndroidRuntime(2561): FATAL EXCEPTION: main
06-01 19:05:28.538: E/AndroidRuntime(2561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.polidoesnttwantacracker/com.example.polidoesnttwantacracker.MainActivity}: java.lang.NullPointerException
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.os.Looper.loop(Looper.java:137)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at java.lang.reflect.Method.invokeNative(Native Method)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at java.lang.reflect.Method.invoke(Method.java:511)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at dalvik.system.NativeStart.main(Native Method)
06-01 19:05:28.538: E/AndroidRuntime(2561): Caused by: java.lang.NullPointerException
06-01 19:05:28.538: E/AndroidRuntime(2561):     at com.example.polidoesnttwantacracker.MainActivity.onCreate(MainActivity.java:23)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.Activity.performCreate(Activity.java:5104)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-01 19:05:28.538: E/AndroidRuntime(2561):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-01 19:05:28.538: E/AndroidRuntime(2561):     ... 11 more

My teacher did it the same way I did it and it ran for him :(

1 Answers1

0

Check the identifiers of the views are properly set in activity_main.xml

If you defined those views in the fragment, then check they are in fragment_main.xml and move the declaration you've done in the onCreate of the activity into the onCreateView of the fragment (using rootView to find them)

Check also you are using the proper theme in your application or activity in AndroidManifest.xml

android:theme="@style/Theme.AppCompat.Light"

Cheers

JAC
  • 287
  • 1
  • 10