Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序中的Android运行时错误_Java_Android - Fatal编程技术网

Java 应用程序中的Android运行时错误

Java 应用程序中的Android运行时错误,java,android,Java,Android,因此,我试图获取实时板球得分,但在我的启动文件中出现以下错误。有人能帮我吗 05-31 08:35:56.833: D/AndroidRuntime(1089): Shutting down VM 05-31 08:35:56.833: W/dalvikvm(1089): threadid=1: thread exiting with uncaught exception (group=0x414c4700) 05-31 08:35:56.843: E/AndroidRuntim

因此,我试图获取实时板球得分,但在我的启动文件中出现以下错误。有人能帮我吗

05-31 08:35:56.833: D/AndroidRuntime(1089): Shutting down VM
    05-31 08:35:56.833: W/dalvikvm(1089): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
    05-31 08:35:56.843: E/AndroidRuntime(1089): FATAL EXCEPTION: main
    05-31 08:35:56.843: E/AndroidRuntime(1089): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cricketscores/com.example.cricketscores.Launching}: java.lang.NullPointerException
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.os.Looper.loop(Looper.java:137)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at java.lang.reflect.Method.invoke(Method.java:525)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at dalvik.system.NativeStart.main(Native Method)
    05-31 08:35:56.843: E/AndroidRuntime(1089): Caused by: java.lang.NullPointerException
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at com.example.cricketscores.Launching.onCreate(Launching.java:52)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.Activity.performCreate(Activity.java:5133)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    05-31 08:35:56.843: E/AndroidRuntime(1089):     ... 11 more
这是代码

public class Launching extends Activity {

        private List<Match> matches = new ArrayList<Match>();
        private MatchesReceiver receiver;
        private IntentFilter filter;
        private MatchListState oldState;
        private MatchAdapter matchAdapter;
        private long lastUpdate;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                CricScoreApplication.getInstance().setMatchListState(MatchListState.LOADING);
                startService(new Intent(this, MatchUpdateService.class));
                this.receiver = new MatchesReceiver();
                this.filter = new IntentFilter(GenericProperties.INTENT_ML_UPDATE);
                this.matchAdapter = new MatchAdapter(this);
                this.oldState = MatchListState.LOADING;
                RunningInfo.clearSession(getApplicationContext());
        }

        @Override
        public void onStart() {
                super.onStart();
        }

        @Override
        public void onResume() {
                super.onResume();
                updateScreen();
                registerReceiver(receiver, filter);
        }

        private void updateScreen() {
                Log.i(GenericProperties.TAG, "updating screen");

                MatchListState state = CricScoreApplication.getInstance()
                                .getMatchListState();

                Resources resources = getResources(); 

                if (MatchListState.LOADING.equals(state)) {
                        setContentView(R.layout.launching_loading);
                        Log.i(GenericProperties.TAG, "updating screen - loading");
                } else if (MatchListState.LOADED.equals(state)) { 
                        showMatches();
                        Log.i(GenericProperties.TAG, "updating screen - show matches");
                } else if (MatchListState.NODATA.equals(state)) {
                        showErrorScreen(resources.getString(R.string.error_title_nodata), resources.getString(R.string.error_msg_nodata));
                        Log.i(GenericProperties.TAG, "updating screen - show error - nodata");
                } else if (MatchListState.NOMATCH.equals(state)) {
                        showErrorScreen(resources.getString(R.string.error_title_nomatch), resources.getString(R.string.error_msg_nomatch));
                        Log.i(GenericProperties.TAG, "updating screen - show error - nomatch");
                } else if (MatchListState.NOTSUPPORTED.equals(state)) {
                        showErrorScreen(resources.getString(R.string.error_title_nosupport), resources.getString(R.string.error_msg_nosupport));
                        Log.i(GenericProperties.TAG, "updating screen - show error - nosupport");
                } else if (MatchListState.APPERROR.equals(state)){
                        showErrorScreen(resources.getString(R.string.error_title_apperror), resources.getString(R.string.error_msg_apperror));
                        Log.i(GenericProperties.TAG, "updating screen - show error - apperror");
                }
                oldState = state;
                Log.i(GenericProperties.TAG, "updating screen ended");

        }

        @Override
        public void onPause() {
                super.onPause();
                unregisterReceiver(receiver);
        }

        @Override
        public void onStop() {
                super.onStop();

        }

        @Override
        public void onDestroy() {
                super.onDestroy();
                stopService(new Intent(this, ScoreUpdateService.class));
        }

        private class MatchesReceiver extends BroadcastReceiver {
                @Override
                public void onReceive(Context context, Intent intent) {
                        long start = System.currentTimeMillis();
                        if(GenericProperties.INTENT_ML_UPDATE.equals(intent.getAction())){
                                updateScreen();
                        }
                        long time = System.currentTimeMillis() - start;
                        Log.i(GenericProperties.TAG, "On Receive Time Taken : " + time
                                        + " ms");
                }
        }

        private void showErrorScreen(String title, String message) {
                if(MatchListState.LOADING.equals(oldState) || MatchListState.LOADED.equals(oldState)){
                        setContentView(R.layout.launching_error);
                }
                Log.v(GenericProperties.TAG, title + " "+ message);
                TextView tilteTextView = (TextView)findViewById(R.id.title);
                tilteTextView.setText(title);
                TextView messageTextView = (TextView)findViewById(R.id.message_body);
                messageTextView.setText(message);

                Log.v(GenericProperties.TAG, " "+ messageTextView.getText());
                Log.v(GenericProperties.TAG, " "+ messageTextView.getTextSize());

        }

        private void showMatches() {

                if(!oldState.equals(MatchListState.LOADED)){
                        setContentView(R.layout.matchlist);
                        final ListView l1 = (ListView) findViewById(R.id.ListMatches);
                        //l1.set
                        l1.setAdapter(matchAdapter);
                        l1.setOnItemClickListener(new OnItemClickListener() {
                                public void onItemClick(AdapterView<?> arg0, View view,
                                                int position, long id) {
                                        int mid = ((Match) l1.getItemAtPosition(position)).getMatchId();
                                        openLiveScore(mid);
                                        openScoreUpdateService(mid);

                                }
                        });     
                }

                long newUpdateTime = CricScoreApplication.getInstance().getMatchListUpdateTime();
                if(newUpdateTime != this.lastUpdate){
                        CricScoreDBAdapter adapter = new CricScoreDBAdapter(
                                        getApplicationContext());
                        adapter.open();
                        this.matches =  adapter.getMatches(); 
                        adapter.close();
                        this.matchAdapter.notifyDataSetChanged();
                        if(this.lastUpdate!=0){
                                Toast.makeText(getApplicationContext(), R.string.toast_update, Toast.LENGTH_SHORT).show();      
                        }

                        this.lastUpdate = newUpdateTime; 

                }

        }

        private void openScoreUpdateService(int mid) {
                Intent serviceIntent = new Intent(
                                GenericProperties.INTENT_START_UPDATE);
                serviceIntent.putExtra("matchId", mid);
                startService(serviceIntent);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.launching_menu, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.hide:
                        moveTaskToBack(false);
                        return true;
                case R.id.exit:
                        //alertAndClose();
                        Launching.this.finish();
                        return true;
                case R.id.feedback:
                        Launching.this.feedback();
                        return true;
                default:
                        return super.onOptionsItemSelected(item);
                }
        }

        private void feedback() {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"spriyan@mychoize.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, "MyChoize CricScore: Feedback");
                i.putExtra(Intent.EXTRA_TEXT   , "Please provide your feedback for improvement of CricScore app here.");
                i.setType("message/rfc822");
                startActivity(Intent.createChooser(i, "Send Feedback"));
        }

        private void openLiveScore(int id) {
                Intent myIntent = new Intent(this, LiveScore.class);
                myIntent.putExtra(GenericProperties.EXTRA_MATCHID, id);
                startActivity(myIntent);
        }

        public void alertAndClose() {
                Resources resources = getResources();
                String str = resources.getString(R.string.confirm_exit);
                String exit = resources.getString(R.string.exit);
                String cancel = resources.getString(R.string.cancel);
                String hide = resources.getString(R.string.hide);
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(str);
                builder.setCancelable(true);
                builder.setNeutralButton(cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                        }
                });
                builder.setPositiveButton(exit, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                                Launching.this.finish();
                        }
                });
                builder.setNegativeButton(hide, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                                moveTaskToBack(false);
                        }
                });
                AlertDialog alert = builder.create();
                alert.show();
        }

        @Override
        public void onBackPressed() {
                alertAndClose();
                return;
        }

        private class MatchAdapter extends BaseAdapter {
                private LayoutInflater mInflater;

                public MatchAdapter(Context context) {
                        mInflater = LayoutInflater.from(context);

                }

                public int getCount() {
                        return Launching.this.matches.size();
                }

                public Object getItem(int position) {
                        return matches.get(position);
                }

                public long getItemId(int position) {
                        return position;
                }

                public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;
                        if (convertView == null) {
                                convertView = mInflater.inflate(R.layout.matchview, null);
                                holder = new ViewHolder();
                                holder.text = (TextView) convertView.findViewById(R.id.TeamOne);
                                holder.text2 = (TextView) convertView
                                                .findViewById(R.id.TeamTwo);
                                convertView.setTag(holder);
                        } else {
                                holder = (ViewHolder) convertView.getTag();
                        }
                        try {
                                Match match = matches.get(position);
                                holder.text.setText(match.getTeamOne());
                                holder.text2.setText(match.getTeamTwo());
                        } catch (ArrayIndexOutOfBoundsException e) {

                        }

                        return convertView;
                }

                class ViewHolder {
                        TextView text;
                        TextView text2;
                }
        }
}
清单



感谢您的帮助

在扩展
应用程序
类时,您不需要使用经典的单例方法。相反,如果清单设置正确,您可以:

@Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //notice the change
                CricScoreApplication app = (CricScoreApplication)getApplication();
                app.setMatchListState(MatchListState.LOADING);

                startService(new Intent(this, MatchUpdateService.class));
                this.receiver = new MatchesReceiver();
                this.filter = new IntentFilter(GenericProperties.INTENT_ML_UPDATE);
                this.matchAdapter = new MatchAdapter(this);
                this.oldState = MatchListState.LOADING;
                RunningInfo.clearSession(getApplicationContext());
        }
要使其正常工作,您需要将该类指向要在基本
应用程序
类上使用的类。这在清单中完成:

<application ... android:name="com.example.cricketscores.CricScoreApplication" >

发布你的strings.xml第52行的内容是什么?
原因:java.lang.NullPointerException 05-31 08:35:56.843:E/AndroidRuntime(1089):在com.example.cricketscores.launting.onCreate(Launching.java:52)
查看第52行,您有一个
null
对象,您正试图访问它的一个properties@SimplePlan第52行是CricCoreApplication.getInstance().setMatchListState(MatchListState.LOADING);可能是
criccoreapplication.getInstance()
正在返回
null
instance…@Mario Thank将尝试它out@Richa
应用程序
标记还需要一个属性(答案中的属性)。换句话说,应用程序标记需要如下所示:
对不起,我的意思是需要使用com.example.cricketscores.criccscoreapplication这个名称的应用程序标记,这是我的错误sorry@MarioStoilov你能帮我看看CricCoreApplication.getInstance()会是什么样子吗:)@MarioStoilov
getApplication()
是一种实例方法,其中as
getInstance()
为静态方法。这有效吗?你怎么能做到?或者您需要
getInstance()
的参数,该参数必须是
Context
。。。
@Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //notice the change
                CricScoreApplication app = (CricScoreApplication)getApplication();
                app.setMatchListState(MatchListState.LOADING);

                startService(new Intent(this, MatchUpdateService.class));
                this.receiver = new MatchesReceiver();
                this.filter = new IntentFilter(GenericProperties.INTENT_ML_UPDATE);
                this.matchAdapter = new MatchAdapter(this);
                this.oldState = MatchListState.LOADING;
                RunningInfo.clearSession(getApplicationContext());
        }
<application ... android:name="com.example.cricketscores.CricScoreApplication" >
public static CricScoreApplication getInstance(Context ctx){
                return (CricScoreApplication)(ctx.getApplication());
        }