簡體   English   中英

為什么我不能在這里開始一個新的活動?

[英]Why can I not start a new Activity here?

我正在嘗試從我的 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

已注冊.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

您似乎在名為 SignedIn 的活動的構造函數中調用findViewById() 您應該稍后調用此方法(例如在onCreate() )並且僅在調用setContentView()之后調用。

嘗試這個..

setContentView之后,您必須在onCreate內部初始化ImageViewTextView如下所示。

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();
        }
    }

以 XML 格式輸入

<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

findViewById在當前視圖層次結構中查找具有 id 的視圖。 您需要先將布局設置為 Activity,然后再初始化視圖。

你需要移動這個

ImageView myPic = (ImageView) findViewById(R.id.imageView1);
TextView myName = (TextView) findViewById(R.id.textView1);

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM