0

我不明白为什么它返回给我这个错误,请帮助我!:(这是错误:

10-31 15:33:28.595: E/Trace(7356): error opening trace file: No such file or directory (2)
10-31 15:33:29.125: E/AndroidRuntime(7356): FATAL EXCEPTION: main
10-31 15:33:29.125: E/AndroidRuntime(7356): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appuniradiocesena/com.example.appuniradiocesena.SwipeyTabsSampleActivity}: java.lang.NullPointerException
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.os.Looper.loop(Looper.java:137)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread.main(ActivityThread.java:4898)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at dalvik.system.NativeStart.main(Native Method)
10-31 15:33:29.125: E/AndroidRuntime(7356): Caused by: java.lang.NullPointerException
10-31 15:33:29.125: E/AndroidRuntime(7356):     at com.example.appuniradiocesena.SwipeyTabsSampleActivity.onCreate(SwipeyTabsSampleActivity.java:77)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.Activity.performCreate(Activity.java:5184)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
10-31 15:33:29.125: E/AndroidRuntime(7356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
10-31 15:33:29.125: E/AndroidRuntime(7356):     ... 11 more

这是我运行时的第一堂课:

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.TimerTask;

import net.moraleboost.streamscraper.ScrapeException;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.scraper.IceCastScraper;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SwipeyTabsSampleActivity extends FragmentActivity {
private Button streamButton;

private ImageButton playButton;

private TextView textStreamed;
private TextView textSong;

private boolean isPlaying;

private StreamingMediaPlayer audioStreamer;

private SwipeyTabs mTabs;
private ViewPager mViewPager;

private static final String [] TITLES = {
    "Live Stream",
    "Palinsesto",
    "Programmi",
    "Eventi",
};




@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_swipeytab);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    mTabs = (SwipeyTabs) findViewById(R.id.swipey_tab);

    SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(this,
            getSupportFragmentManager());
    mViewPager.setAdapter(adapter);
    mTabs.setAdapter(adapter);
    mViewPager.setOnPageChangeListener(mTabs);
    mViewPager.setCurrentItem(0);
/*
    Inizializza();

    //new AsyncTaskProc().execute();

    //new Timer().schedule(new IceCastPoll(), 1000);
     */
}

private void startStreamingAudio() {
    try { 
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        if ( audioStreamer != null) {
            audioStreamer.interrupt();
        }


        audioStreamer = new StreamingMediaPlayer(this, textStreamed, playButton, streamButton,progressBar);
        audioStreamer.startStreaming("http://r35798.ovh.net:8000/listen",1677, 214);
        streamButton.setEnabled(false);
    } catch (IOException e) {               
    }

}
private void Inizializza()
{
    textStreamed = (TextView) findViewById(R.id.text_kb_streamed);
    streamButton = (Button) findViewById(R.id.button_stream);
    streamButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            startStreamingAudio();
        }});


    playButton = (ImageButton) findViewById(R.id.button_play);
    playButton.setEnabled(false);
    playButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (audioStreamer.getMediaPlayer().isPlaying()) {
                audioStreamer.getMediaPlayer().pause();
                playButton.setImageResource(R.drawable.button_play);
            } else {
                audioStreamer.getMediaPlayer().start();

                audioStreamer.startPlayProgressUpdater();
                playButton.setImageResource(R.drawable.button_pause);
            }
            isPlaying = !isPlaying;
        }});
}
private class SwipeyTabsPagerAdapter extends FragmentPagerAdapter implements
SwipeyTabsAdapter {

    private final Context mContext;

    public SwipeyTabsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);

        this.mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        return SwipeyTabFragment.newInstance(TITLES[position]);
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    public TextView getTab(final int position, SwipeyTabs root) {
        TextView view = (TextView) LayoutInflater.from(mContext).inflate(
                R.layout.swipey_tab_indicator, root, false);
        view.setText(TITLES[position]);
        view.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mViewPager.setCurrentItem(position);
            }
        });

        return view;
    }

}


public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub

}
//////////////////////////////////////

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Uscita").setOnMenuItemClickListener(
            new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(getApplicationContext(),
                            "Uscita Programma", Toast.LENGTH_SHORT).show();
                    finish();
                    System.exit(0);

                    return true;
                }
            });
    ;


    return true;
}

public class IceCastPoll extends TimerTask {

    public IceCastPoll() {

    }

    @TargetApi(9)
    public void run() {
        Scraper scraper = new IceCastScraper();
        try {

            List<Stream> streams=null;
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            streams = scraper.scrape(new URI("http://r35798.ovh.net:8000"));            
            for (Stream stream: streams) {
                Toast.makeText(getApplicationContext(),stream.getCurrentSong(), Toast.LENGTH_LONG).show();
                //textSong.setText((stream.getCurrentSong()));
            }
        }
        catch (URISyntaxException e) {

            System.out.println(e.getMessage());
        }
        catch (ScrapeException e) {

            System.out.println(e.getMessage());
        }

    }

}


class AsyncTaskProc extends AsyncTask<Void, String, Void> {
    List<Stream> streams=null;

    protected void onPostExecute()
    {

        textSong =(TextView) findViewById(R.id.textViewCurrentSong);

        try {
            for (Stream stream: streams) {

                textSong.setText((stream.getCurrentSong()));
            }
        } catch (Exception e) {
            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();

        }

    }
    @TargetApi(9)
    @Override
    protected Void doInBackground(Void... unused) {



        Scraper scraper = new IceCastScraper();

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        try {
            streams = scraper.scrape(new URI("http://r35798.ovh.net:8000/"));
        } catch (ScrapeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        }




        return (null);
    }


}
}

当我尝试编译和运行应用程序崩溃时,我不明白为什么。谢谢;

4

1 回答 1

1

这是堆栈的重要部分:

Caused by: java.lang.NullPointerException
    at com.example.appuniradiocesena.SwipeyTabsSampleActivity.onCreate(SwipeyTabsSampleActivity.java:77)

--> 你应该NullPointerException通过查看SwipeyTabsSampleActivity.java.

也许您正在调用findViewById()布局中不存在的 ID?(这将使该方法返回null

于 2012-10-31T14:50:20.230 回答