[英]Why can I not start a new Activity here?
I am trying to start a ListActivity from my MainActivity, I have it declared in the manifest but its giving me a NullPointerException on SignedUp我正在尝试从我的 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();
}
}
Stack trace:堆栈跟踪:
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:已注册.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: 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>
Thanks for all the help!感谢所有的帮助! Here is logcat that comes from SignUp now:这是现在来自 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
You appear to be calling findViewById()
inside the constructor of an Activity called SignedIn.您似乎在名为 SignedIn 的活动的构造函数中调用findViewById()
。 You should call this method later (such as in onCreate()
) and only after calling setContentView()
.您应该稍后调用此方法(例如在onCreate()
)并且仅在调用setContentView()
之后调用。
Try this..尝试这个..
You have to initialize your ImageView
and TextView
inside onCreate
after setContentView
like below.在setContentView
之后,您必须在onCreate
内部初始化ImageView
和TextView
如下所示。
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);
}
EDIT编辑
@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以 XML 格式输入
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
findViewById
looks for a view with the id in the current view hierarchy. findViewById
在当前视图层次结构中查找具有 id 的视图。 You need to set the layout to the Activity first and then initialize views.您需要先将布局设置为 Activity,然后再初始化视图。
You need to move this你需要移动这个
ImageView myPic = (ImageView) findViewById(R.id.imageView1);
TextView myName = (TextView) findViewById(R.id.textView1);
inside onCreate
after setContentView
.在setContentView
之后的onCreate
内。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.