Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android版Eclipse应用程序,新手_Java_Android_Eclipse - Fatal编程技术网

Java android版Eclipse应用程序,新手

Java android版Eclipse应用程序,新手,java,android,eclipse,Java,Android,Eclipse,我开始学习如何为android开发应用程序。这是我的第一个项目(测试),它一直给我同样的错误。。。“应用程序意外停止”,我不知道问题出在哪里 MainActivity.java package com.example.layoutxml2; >import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import an

我开始学习如何为android开发应用程序。这是我的第一个项目(测试),它一直给我同样的错误。。。“应用程序意外停止”,我不知道问题出在哪里

MainActivity.java

package com.example.layoutxml2;

>import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
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 {

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

            final TextView text1 = (TextView) findViewById(R.id.textView1);
            Button carregar = (Button) findViewById(R.id.button1);

            carregar.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    text1.setText("Xau Pessoal!");

                }
            });


        }
    }

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

}
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: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.layoutxml2.MainActivity$PlaceholderFragment"
    android:background="#285178" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/banner"
        android:src="@drawable/androidbanner" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/saudacao"
        android:textSize="26sp"
        android:textColor="#ffffff"
         />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:text="@string/carregar"
        android:textColor="#ffffff"
        android:onClick="selfdestruct" />

</RelativeLayout>
如果错误很明显,我很抱歉,但我对这方面很新,我想改进。 谢谢你的帮助:) 当做
Ricardo Moreira

您在片段布局中声明了按钮,但在活动中使用了它们。将代码移到片段类中的onCreateView方法中,并使用rootview.findViewById()


你读过stacktrace吗?在第28行的
onCreate
方法中有一个
NullPointerException
。找出编辑器中的哪一行,然后确定哪一行为空以及为什么为空。如果我不得不猜测的话,我会说是这一行
carregar.setOnClickListener(newview.OnClickListener(){
我不确定,因为我还没有使用
ActionBarActivity
,但是在声明中说你需要一个
onCreateView()
方法“将代码移到片段类的onCreateView类中,并使用rootview.findViewById()…”是指将代码
final TextView text1=(TextView)findViewById(R.id.textView1);Button carregar=(Button)findViewById(R.id.button1);carregar.setOnClickListener(new View.OnClickListener()){@Override public void onClick(视图v){//TODO自动生成的方法存根text1.setText(“@string/xau”);})
插入fragment_main.xml并在CreateView上创建一个类?我编辑了上面的帖子。很抱歉昨天的回答太快,但我是从平板电脑上写的。将代码移动到片段的原因是您在布局中声明了文本视图和按钮资源,而不是为您的活动。当您夸大您的行为时在活动的布局中,找不到资源,因为它们未在此布局中声明。因此,当您在片段的onCreateView方法中展开片段布局时,可以获取它们。
06-14 16:33:35.084: E/AndroidRuntime(1628): FATAL EXCEPTION: main
06-14 16:33:35.084: E/AndroidRuntime(1628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layoutxml2/com.example.layoutxml2.MainActivity}: java.lang.NullPointerException
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.os.Looper.loop(Looper.java:130)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at java.lang.reflect.Method.invoke(Method.java:507)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at dalvik.system.NativeStart.main(Native Method)
06-14 16:33:35.084: E/AndroidRuntime(1628): Caused by: java.lang.NullPointerException
06-14 16:33:35.084: E/AndroidRuntime(1628):     at com.example.layoutxml2.MainActivity.onCreate(MainActivity.java:28)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 16:33:35.084: E/AndroidRuntime(1628):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 16:33:35.084: E/AndroidRuntime(1628):     ... 11 more
package com.example.layoutxml2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
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 {

    @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() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

            final TextView text1 = (TextView) rootView.findViewById(R.id.textView1);
            Button carregar = (Button) rootView.findViewById(R.id.button1);

            carregar.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    text1.setText("Xau Pessoal!");

                }
            });

            return rootView;
        }
    }
}