Admob不断崩溃我的应用程序

时间:2014-08-11 17:09:02

标签: java android admob

我的应用程序使用工作正常,直到我添加了admob。我无法弄清楚为什么它一直在崩溃。 如果您需要更多信息,请告诉我,我会发布。 抱歉没有LogCat,因为Eclipse不会运行adb。

主要的java

     public class MainActivity extends ActionBarActivity {
            ArrayList<String> lines = new ArrayList<String>();
            ListView list_view;
            ArrayAdapter<String> adapter;
            private AdView adView;
            @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();
                }

                adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lines);
                list_view.setAdapter(adapter);
                lines.add("Proxies");
            } 
   public void Read_Proxy() throws IOException
    {
        String filename = "/mnt/sdcard/Download/proxy.txt";
        InputStream in = null;
        FileInputStream fstream = new FileInputStream(filename);
        lines.clear();
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        while ((strLine = br.readLine()) != null) 
        {
            lines.add(strLine);
            adapter.notifyDataSetChanged();
        }
        br.close();     

    }

    public void Download_Proxy(View view)
    {
        int count;
        try {
            URL url = new URL("website.com");
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            InputStream is = url.openStream();
            File testDirectory = new File(
                    Environment.getExternalStorageDirectory() + "/Download");
            File file = new File (Environment.getExternalStorageDirectory() + "/Download/proxy.txt");

            if (!testDirectory.exists()) {
                testDirectory.mkdir();
            }
            if (file.exists())
            {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(testDirectory + "/proxy.txt");
            byte data[] = new byte[1024];
            long total = 0;
            int progress = 0;
            while ((count = is.read(data)) != -1) 
            {
                total += count;
                int progress_temp = (int) total * 100 / lenghtOfFile;
                fos.write(data, 0, count);
            }
            is.close();
            fos.close();
          //  Read_Proxy();

        } catch (Exception e) {
            Log.e("ERROR DOWNLOADING",
                    "Unable to download" + e.getMessage());
        }
    }

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/proxy_list"
        android:layout_width="match_parent"
        android:layout_height="319dp"
        android:layout_weight="0.82" >
    </ListView>
   <com.google.android.gms.ads.AdView android:id="@+id/adView"
                               android:layout_width="match_parent"
                               android:layout_height="wrap_content"
                               ads:adSize="BANNER"
                               ads:adUnitId="XXXXXXXXXXXXXX"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:onClick="Download_Proxy"
        android:text="Get Proxies" />

</LinearLayout>

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unfinishedgaming.proxylist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
           <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />

    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

1 个答案:

答案 0 :(得分:0)

list_view中的list_view.setAdapter(adapter);为空,因为您没有对其进行初始化并将适配器设置为null list_view。

并添加

adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

onCreate方法中,当您在xml文件中使用adView但忘记在活动中使用adView时。

有关详细信息,请参阅googleads-mobile-android-banner-xml examples