0

我是android开发的新手,为了更好地理解片段,我正在学习一个教程,但是在完成了测试教程后,我在删除了一些语法错误后遇到了这个特殊问题。在 logcat 中我得到“ threadid=1: thread exiting with uncaught exception (group=0x40a7193 ” 我已经花了几乎一整天的时间在上面,我看过但似乎这个问题的解决方案取决于源代码,我一直在许多链接上,但到处都提供了一些源代码,到目前为止我还没有找到解决我的问题的方法,任何帮助将不胜感激。这是我的代码:

MainActivity.java:

package com.example.fragmentsexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements ToolbarFragments.ToolbarListener{

    @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.main, menu);
        return true;
    }

    @Override
    public void onButtonClick(int fontsize, String text) {
        TextFragment textFragment = 
                (TextFragment) getSupportFragmentManager().findFragmentById(R.id.text_fragment);        
        textFragment.changeTextProperties(fontsize, text);
    }
}

这是TextFragment.java:

package com.example.fragmentsexample;

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

public class TextFragment extends Fragment {

    private static TextView textview;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.text_fragment, container,false);
        textview=(TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void changeTextProperties(int fontsize, String text){
        textview.setTextSize(fontsize);
        textview.setText(text);
    }
}

这是 ToolbarFragments.java

package com.example.fragmentsexample;

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

public class ToolbarFragments extends Fragment {

    private static int seekvalue=10;
    private static EditText editText;

    ToolbarListener activityCallback;

    public interface ToolbarListener {
        public void onButtonClick(int position, String text);
    }

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        try{
            activityCallback=(ToolbarListener) activity;
        }       
        catch (Exception e){
            throw new ClassCastException(activity.toString()+ "must implement ToolbarListener");
        }
    }

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

        editText=(EditText) view.findViewById(R.id.editText1);
        final SeekBar seekbar=(SeekBar) view.findViewById(R.id.seekBar1);
        seekbar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this);
        final Button button=(Button) view.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {      
            @Override
            public void onClick(View v) {
                buttonClicked(v);
            }
        });

        return view;
    }

    public void buttonClicked(View v){
        activityCallback.onButtonClick(seekvalue,editText.getText().toString());    
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
        seekvalue=progress;
    }

    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
    }

    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
    }
}

这是activity_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"
    tools:context=".MainActivity" >

<fragment
    android:id="@+id/toolbar_fragment"
    android:name="com.example.fragmentexample.ToolbarFragments"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    tools:layout="@layout/toolbar_fragment" />

<fragment
    android:id="@+id/text_fragment"
    android:name="com.example.fragmentexample.TextFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    tools:layout="@layout/text_fragment" />
</RelativeLayout>

这是 text_fragment.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" >

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="@string/fragtwo"
       android:textAppearance="?android:attr/textAppearanceLarge" />   
</RelativeLayout>

这是toolbar_fragment.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" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/seekBar1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:text="@string/button"/>

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="text" >
    <requestFocus />
</EditText>

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="14dp" />

</RelativeLayout>

任何帮助任何形式的帮助将不胜感激这是我在这里的第二篇文章我为一个凌乱的帖子道歉

4

0 回答 0