我正在尝试从我的MainActivity启动ListActivity,我在清单中声明了它,但它在SignedUp上给了我一个NullPointerException
public void logn(View v) {
Boolean[] result = {false};
LogIn log = (LogIn) new LogIn().execute(result);
if (result[0].equals(true)){
Intent intent = new Intent(this, SignedIn.class);
startActivity(intent);
finish();
}
}
堆栈追踪:
04-15 00:59:44.884: E/AndroidRuntime(12371): FATAL EXCEPTION: main
04-15 00:59:44.884: E/AndroidRuntime(12371): Process: com.example.whiteboard, PID: 12371
04-15 00:59:44.884: E/AndroidRuntime(12371): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.whiteboard/com.example.whiteboard.SignedIn}: java.lang.NullPointerException
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.os.Looper.loop(Looper.java:136)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 00:59:44.884: E/AndroidRuntime(12371): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 00:59:44.884: E/AndroidRuntime(12371): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 00:59:44.884: E/AndroidRuntime(12371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 00:59:44.884: E/AndroidRuntime(12371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 00:59:44.884: E/AndroidRuntime(12371): at dalvik.system.NativeStart.main(Native Method)
04-15 00:59:44.884: E/AndroidRuntime(12371): Caused by: java.lang.NullPointerException
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.Activity.findViewById(Activity.java:1884)
04-15 00:59:44.884: E/AndroidRuntime(12371): at com.example.whiteboard.SignedIn.<init>(SignedIn.java:25)
04-15 00:59:44.884: E/AndroidRuntime(12371): at java.lang.Class.newInstanceImpl(Native Method)
04-15 00:59:44.884: E/AndroidRuntime(12371): at java.lang.Class.newInstance(Class.java:1208)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-15 00:59:44.884: E/AndroidRuntime(12371): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-15 00:59:44.884: E/AndroidRuntime(12371): ... 11 more
SignedUp.java:
package com.example.whiteboard;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.mysql.jdbc.Connection;
public class SignedIn extends ListActivity {
public int userId;
public List<RowItem> data = new ArrayList<RowItem>();
ImageView myPic = (ImageView) findViewById(R.id.imageView1);
TextView myName = (TextView) findViewById(R.id.textView1);
String name;
Bitmap profpic;
String thePicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle bnd = getIntent().getExtras();
userId = bnd.getInt("userid");
name = bnd.getString("name");
thePicture = bnd.getString("mypic");
byte[] encodeByte = Base64.decode(thePicture,
Base64.DEFAULT);
profpic = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
myPic.setImageBitmap(profpic);
myName.setText(name);
setContentView(R.layout.owners_info_layout);
// LoadRows lr = new LoadRows();
// lr.execute();
}
private class LoadRows extends AsyncTask<Void, Void, String> {
String success = "success";
@Override
protected String doInBackground(Void... params) {
try {
String url = //////;
String dbName = ////;
String driver = "com.mysql.jdbc.Driver";
String userName = /////;
String password = /////;
Connection conn;
Class.forName(driver).newInstance();
conn = (Connection) DriverManager.getConnection(url + dbName,
userName, password);
java.sql.PreparedStatement checkUserInfo = conn
.prepareStatement("SELECT * FROM rowitem WHERE profileto = ? ");
checkUserInfo.setInt(1, userId);
ResultSet rs = checkUserInfo.executeQuery();
while(rs.next()){
RowItem item = new RowItem();
item.setJustpic(rs.getBoolean("ispic"));
item.setJusttext(rs.getBoolean("isText"));
item.setProfilefrom(rs.getInt("profilefrom"));
java.sql.PreparedStatement checkUserInfo2 = conn.prepareStatement("SELECT * FROM profiles WHERE profileid = ? ORDER by date desc");
checkUserInfo2.setInt(1, item.getProfilefrom());
ResultSet temp = checkUserInfo2.executeQuery();
item.setProfilepic(temp.getString("profilepic"));
if(item.isJusttext()){
item.setText(rs.getString("text"));
// break;
}
else if(item.isJustpic()){
item.setPostedpic(rs.getString("pic"));
// break;
}
else{
item.setText(rs.getString("text"));
item.setPostedpic(rs.getString("pic"));
}
data.add(item);
}
conn.close();
return success;
} catch (Exception e) {
e.printStackTrace();
// conn.close();
return null;
}
}
@Override
protected void onPostExecute(String result) {
if (result.equals(success)){
ListView listview = (ListView) findViewById(R.id.list);
CustomAdapter adapt = new CustomAdapter(SignedIn.this, R.layout.toplayout, data);
listview.setAdapter(adapt);
}
else {
Toast.makeText(getApplicationContext(), "You have no posts yet!", Toast.LENGTH_LONG).show();
}
}
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.whiteboard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/rowlogo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.whiteboard.SplashScreen"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.whiteboard.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.example.whiteboard.SignedIn"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden" >
</activity>
</application>
</manifest>
感谢您的帮助! 这是现在来自SignUp的logcat:
04-15 01:26:51.494: E/AndroidRuntime(14589): FATAL EXCEPTION: main
04-15 01:26:51.494: E/AndroidRuntime(14589): Process: com.example.whiteboard, PID: 14589
04-15 01:26:51.494: E/AndroidRuntime(14589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whiteboard/com.example.whiteboard.SignedIn}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.os.Looper.loop(Looper.java:136)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 01:26:51.494: E/AndroidRuntime(14589): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 01:26:51.494: E/AndroidRuntime(14589): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 01:26:51.494: E/AndroidRuntime(14589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 01:26:51.494: E/AndroidRuntime(14589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 01:26:51.494: E/AndroidRuntime(14589): at dalvik.system.NativeStart.main(Native Method)
04-15 01:26:51.494: E/AndroidRuntime(14589): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-15 01:26:51.494: E/AndroidRuntime(14589): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.Activity.setContentView(Activity.java:1929)
04-15 01:26:51.494: E/AndroidRuntime(14589): at com.example.whiteboard.SignedIn.onCreate(SignedIn.java:35)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.Activity.performCreate(Activity.java:5231)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 01:26:51.494: E/AndroidRuntime(14589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-15 01:26:51.494: E/AndroidRuntime(14589): ... 11 more
答案 0 :(得分:2)
您似乎在名为SignedIn的Activity的构造函数内调用findViewById()
。您应该稍后调用此方法(例如在onCreate()
中)并且仅在调用setContentView()
之后调用此方法。
答案 1 :(得分:2)
试试这个..
您必须在ImageView
之后的TextView
内onCreate
和setContentView
初始化。
public class SignedIn extends ListActivity {
public int userId;
public List<RowItem> data = new ArrayList<RowItem>();
ImageView myPic;
TextView myName;
String name;
Bitmap profpic;
String thePicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.owners_info_layout);
myPic = (ImageView) findViewById(R.id.imageView1);
myName = (TextView) findViewById(R.id.textView1);
Bundle bnd = getIntent().getExtras();
userId = bnd.getInt("userid");
name = bnd.getString("name");
thePicture = bnd.getString("mypic");
byte[] encodeByte = Base64.decode(thePicture,
Base64.DEFAULT);
profpic = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
myPic.setImageBitmap(profpic);
myName.setText(name);
}
修改强>
@Override
protected void onPostExecute(String result) {
if (result.equals(success)){
ListView listview = getListView();
CustomAdapter adapt = new CustomAdapter(SignedIn.this, R.layout.toplayout, data);
listview.setAdapter(adapt);
}
else {
Toast.makeText(getApplicationContext(), "You have no posts yet!", Toast.LENGTH_LONG).show();
}
}
IN XML
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
答案 2 :(得分:1)
findViewById
在当前视图层次结构中查找具有id的视图。您需要先将布局设置为Activity,然后初始化视图。
你需要移动这个
ImageView myPic = (ImageView) findViewById(R.id.imageView1);
TextView myName = (TextView) findViewById(R.id.textView1);
onCreate
之后的setContentView
内的。
ImageView myPic;
TextView myName ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle bnd = getIntent().getExtras();
userId = bnd.getInt("userid");
name = bnd.getString("name");
thePicture = bnd.getString("mypic");
byte[] encodeByte = Base64.decode(thePicture,
Base64.DEFAULT);
profpic = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
setContentView(R.layout.owners_info_layout);
ImageView myPic = (ImageView) findViewById(R.id.imageView1);
TextView myName = (TextView) findViewById(R.id.textView1);
myPic.setImageBitmap(profpic);
myName.setText(name);