gpt4 book ai didi

java - Android - 空指针异常 : storage == null

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:01 32 4
gpt4 key购买 nike

您好,我在使用在线教程构建的 Android 应用程序时遇到问题,当我尝试运行它并出现上述异常时,它一直崩溃。请就如何停止此错误提供一些帮助,我将不胜感激。这是我的主要 Activity :

package com.niall.easytxt;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

private ChatArrayAdapter adp;
private ListView list;
private EditText chatText;
private Button send;
Intent in;
private boolean side = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent i = getIntent();
send = (Button)findViewById(R.id.btn);
list = (ListView)findViewById(R.id.listview);
adp = new ChatArrayAdapter(getApplicationContext(),R.layout.chat, null);
chatText = (EditText)findViewById(R.id.chat);
chatText.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event){
if((event.getAction()==KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
{
return sendChatMessage();
}


return false;
}






});
send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

sendChatMessage();
}
});

list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
list.setAdapter(adp);
adp.registerDataSetObserver(new DataSetObserver() {
public void OnChanged(){
super.onChanged();
list.setSelection(adp.getCount() -1);
}

});


}


private boolean sendChatMessage() {


adp.add(new ChatMessage(side,chatText.getText().toString()));
chatText.setText("");

side =! side;


return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

还有我的 ChatArrayAdapter:

package com.niall.easytxt;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChatArrayAdapter extends ArrayAdapter<ChatMessage>{
private TextView chatText;
private List<ChatMessage> MessageList = new ArrayList<ChatMessage>();
private LinearLayout layout;




public ChatArrayAdapter(Context context, int resource, ChatMessage[] objects) {
super(context, resource, objects);

}

public void add(ChatMessage object) {

MessageList.add(object);
super.add(object);
}
public int getCount()
{
return this.MessageList.size();
}
public ChatMessage getItem(int index)
{

return this.MessageList.get(index);

}
public View getView(int position, View ConvertView, ViewGroup parent)
{
View v = ConvertView;
if(v==null){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v =inflater.inflate(R.layout.chat, parent,false);
}

layout = (LinearLayout)v.findViewById(R.id.Message1);
ChatMessage messageobj = getItem(position);
chatText = (TextView)v.findViewById(R.id.SingleMessage);

chatText.setText(messageobj.message);
chatText.setBackgroundResource(messageobj.left ? R.drawable.bubble_a :R.drawable.bubble_b);
layout.setGravity(messageobj.left?Gravity.START:Gravity.END);

return v;

}
public Bitmap decodeToBitmap(byte[] decodedByte){
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

}

这是日志

04-29 05:40:03.988: D/AndroidRuntime(1794): Shutting down VM
04-29 05:40:03.988: E/AndroidRuntime(1794): FATAL EXCEPTION: main
04-29 05:40:03.988: E/AndroidRuntime(1794): Process: com.niall.easytxt, PID: 1794
04-29 05:40:03.988: E/AndroidRuntime(1794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.niall.easytxt/com.niall.easytxt.MainActivity}: java.lang.NullPointerException: storage == null
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.os.Handler.dispatchMessage(Handler.java:102)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.os.Looper.loop(Looper.java:135)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-29 05:40:03.988: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Native Method)
04-29 05:40:03.988: E/AndroidRuntime(1794): at java.lang.reflect.Method.invoke(Method.java:372)
04-29 05:40:03.988: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-29 05:40:03.988: E/AndroidRuntime(1794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-29 05:40:03.988: E/AndroidRuntime(1794): Caused by: java.lang.NullPointerException: storage == null
04-29 05:40:03.988: E/AndroidRuntime(1794): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
04-29 05:40:03.988: E/AndroidRuntime(1794): at java.util.Arrays.asList(Arrays.java:155)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
04-29 05:40:03.988: E/AndroidRuntime(1794): at com.niall.easytxt.ChatArrayAdapter.<init>(ChatArrayAdapter.java:26)
04-29 05:40:03.988: E/AndroidRuntime(1794): at com.niall.easytxt.MainActivity.onCreate(MainActivity.java:33)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.Activity.performCreate(Activity.java:5933)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-29 05:40:03.988: E/AndroidRuntime(1794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-29 05:40:03.988: E/AndroidRuntime(1794): ... 10 more

最佳答案

adp = new ChatArrayAdapter(getApplicationContext(),R.layout.chat, null);

你的问题出在这条线上。您将 null 作为数据集传递。 ChatMessage[] 的空引用。 super 构造函数调用

Arrays.asList(objects)

导致异常的原因。要修复它,请提供一个空数据集。例如,您可以像这样更改您的构造函数:

public ChatArrayAdapter(Context context, int resource) {
super(context, resource, MessageList);
}

关于java - Android - 空指针异常 : storage == null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939760/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com