更新
像个白痴一样,我没有注意logCat。
问题出在我浏览的页面上,而不是意图本身,我在setContentView代码有机会在onContentLoad()中运行之前声明了一些代码。
感谢您的所有帮助!
我知道以前曾问过这个问题,但是我关注了无数关于该主题的帖子。但似乎找不到正确的答案。
我正在尝试从MainActivity页面导航至ViewFeeds页面;但它引发了以上错误。
有趣的是,我已经有了在页面之间导航的代码,并且可以正常工作。但是,当我为另一个类复制此代码时,它将引发以上错误。
MainActivity代码(我正在浏览的代码):
public void btnFeedsClick(View v)
{
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ViewFeeds.class);
MainActivity.this.startActivity(intent);
}
});
}
ViewFeeds类(我正在导航的类):
package com.example.rssapplication;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewFeeds extends Activity{
//The array which holds all of the data...
ArrayList<String> items = new ArrayList<String>();
ListView lv = (ListView) findViewById(R.id.listView1);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewfeeds);
ActionBar actionBar = getActionBar();
actionBar.hide();
items = new ArrayList<String>();
String ret = "";
try {
InputStream inputStream = openFileInput("Teams.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
// Toast.makeText(MainActivity.this, receiveString, Toast.LENGTH_LONG).show();
try {
while ( (receiveString = bufferedReader.readLine()) != null ) {
items.add(receiveString);
stringBuilder.append(receiveString + "\r\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputStream.close();
//ret holds each line of text from the file...
ret = stringBuilder.toString();
TextView txt = (TextView) findViewById(R.id.textView6);
txt.setText(ret);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
items);
lv.setAdapter(arrayAdapter);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
android Manifest(我在声明引用的部分上方添加了注释):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rssapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application
android:allowBackup="true"
android:icon="@drawable/sportsoccericon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:screenOrientation="portrait"
>
<activity
android:name="com.example.rssapplication.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.example.rssapplication.SelectTeams" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- This is where I am creating the intent to navigate from main, to viewfeeds -->
<activity android:name="com.example.rssapplication.ViewFeeds" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.rssapplication.ShowArticle" android:label="@string/app_name"
android:parentActivityName="com.example.rssapplication.MyAdapter"
>
<!-- <meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.rssapplication.MyAdapter" />-->
<!-- <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> -->
</activity>
</application>
</manifest>
这是他的日志:
08-07 16:27:02.512: E/AndroidRuntime(2321): FATAL EXCEPTION: main
08-07 16:27:02.512: E/AndroidRuntime(2321): Process: com.example.rssapplication, PID: 2321
08-07 16:27:02.512: E/AndroidRuntime(2321): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.rssapplication/com.example.rssapplication.ViewFeeds}: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.os.Looper.loop(Looper.java:157)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-07 16:27:02.512: E/AndroidRuntime(2321): at dalvik.system.NativeStart.main(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): Caused by: java.lang.NullPointerException
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.Activity.findViewById(Activity.java:1965)
08-07 16:27:02.512: E/AndroidRuntime(2321): at com.example.rssapplication.ViewFeeds.<init>(ViewFeeds.java:20)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.Class.newInstanceImpl(Native Method)
08-07 16:27:02.512: E/AndroidRuntime(2321): at java.lang.Class.newInstance(Class.java:1208)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
08-07 16:27:02.512: E/AndroidRuntime(2321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
08-07 16:27:02.512: E/AndroidRuntime(2321): ... 11 more
任何帮助将不胜感激!
谢谢,
卡勒姆
最佳答案
问题是这样的:
ListView lv = (ListView) findViewById(R.id.listView1);
您太早拨打
findViewById()
了!您始终必须注意Activity Lifecycle!只有在使用findViewById()
设置布局后,才能调用setContentView()
。您应该像这样在
Views
中寻找onCreate()
:public class ViewFeeds extends Activity {
private ArrayList<String> items = new ArrayList<String>();
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewfeeds);
lv = (ListView) findViewById(R.id.listView1);
...
}
...
}