简单的Android应用程序不会运行

时间:2014-06-01 18:48:06

标签: java android

每当我尝试运行我在Eclipse中制作的Android应用程序时,它就会让我停止工作。 这是一个简单的按钮和文本视图文本设置。这是我的代码。

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

 }

请求的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.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>

所以,如果你能帮助我。这真的令人沮丧,如果我没有让它继续运行,我就无法继续我的演绎。

这是它给我的猫日志: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

我的老师就像我做的那样做了它并为他跑了:(

1 个答案:

答案 0 :(得分:0)

检查视图的标识符是否在activity_main.xml中正确设置

如果您在片段中定义了这些视图,那么请检查它们是否在fragment_main.xml中,并将您在活动的onCreate中完成的声明移动到片段的onCreateView中(使用rootView查找它们)< / p>

同时检查您在AndroidManifest.xml中的应用程序或活动中是否使用了正确的主题

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

干杯