android中的第一个片段应用程序有很多错误

时间:2015-07-09 20:48:58

标签: android android-fragments

我刚开始学习android,我想用片段构建一个应用程序 但我有太多的错误,我不知道从哪里开始调试 作为一个初学者我想请求帮助找到bug / bug并知道如何调试这类问题在这里输入代码

07-09 23:16:45.075    8822-8822/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
07-09 23:16:45.273    8822-8822/com.example.android.myapplication D/AndroidRuntime﹕ Shutting down VM
07-09 23:16:45.274    8822-8822/com.example.android.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.myapplication, PID: 8822
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.myapplication/com.example.android.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.android.myapplication.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.example.android.myapplication.TopFragment.onCreateView(TopFragment.java:54)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
            at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838)
            at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.android.myapplication.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-09 23:16:45.282    8822-8834/com.example.android.myapplication I/art﹕ Background partial concurrent mark sweep GC freed 172(20KB) AllocSpace objects, 0(0B) LOS objects, 34% free, 7MB/11MB, paused 15.024ms total 30.293ms
07-09 23:18:50.643    8822-8829/com.example.android.myapplication W/art﹕ Suspending all threads took: 5.642ms

BottomFragment

package com.example.android.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* Created by 0110 on 09/07/2015.
*/
public class BottomFragment extends Fragment {

private static TextView CenterView;
private static TextView BottomView;


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

    CenterView = (TextView) view.findViewById(R.id.firstText);
    BottomView = (TextView) view.findViewById(R.id.secondText);





    return view;
}


public void setMeme(String top, String Bottom){

    CenterView.setText(top);
    BottomView.setText(Bottom);
}

}

TopFragment

package com.example.android.myapplication;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by 0110 on 08/07/2015.
 */


public class TopFragment extends Fragment {


public static EditText first;
public static EditText second;


TopFragListener activityCommander;

public interface TopFragListener{
     void CreatMeme(String top, String Bottom);
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activityCommander = (TopFragListener)activity;
    }catch (ClassCastException e){
        throw new ClassCastException(activity.toString());
    }
}

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


    first = (EditText) view.findViewById(R.id.editText);
    second = (EditText)view.findViewById(R.id.editText2);
    final Button button = (Button) view.findViewById(R.id.button);


    button.setOnClickListener(
            new View.OnClickListener(){
                public void onClick(View v){
                    buttonClicked(v);
                }
            }
    );



    return view;
}


public void buttonClicked(View v){
    activityCommander.CreatMeme(first.getText().toString(),second.getText().toString());

}

}

MainActivity

package com.example.android.myapplication;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;



public class MainActivity extends ActionBarActivity implements     TopFragment.TopFragListener{


@Override
public void CreatMeme(String top, String Bottom) {
    BottomFragment BottomFragmenT = (BottomFragment)  getSupportFragmentManager().findFragmentById(R.id.fragment2);
    BottomFragmenT.setMeme(top,Bottom);


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

MainActivity 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:orientation="vertical"

tools:context=".MainActivity">

<fragment
    android:layout_width="match_parent"
    android:layout_height="240dp"
    android:name="com.example.android.myapplication.TopFragment"
    android:id="@+id/fragment"

    tools:layout="@layout/topfragment"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp" />

<fragment
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:name="com.example.android.myapplication.BottomFragment"
    android:id="@+id/fragment2"
    android:layout_gravity="center_horizontal"

    tools:layout="@layout/bottomframgent"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_below="@+id/fragment" />
</RelativeLayout>

Topfragment xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/topFrag">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:text="@string/toptext"
    android:layout_marginTop="20dp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText2"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:gravity="center_horizontal"
    android:text="@string/bottomtext" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonText"
    android:id="@+id/button"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

bottemfragment xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/abcdef"
android:id="@+id/bottomFrag">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="CenterView"
    android:gravity="center_horizontal"
    android:id="@+id/firstText"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textColor="#fff"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="BottomText"
    android:gravity="center_horizontal"

    android:id="@+id/secondText"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textColor="#fff" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你在TopFragment中冒错了xml文件:

View view = inflater.inflate(R.layout.bottomframgent,container,false);

而不是

 View view = inflater.inflate(R.layout.topfragment,container,false);

,反之亦然底部片段